sendJson
Low-level JSON serialization/send helper. Inside a route, prefer socket.sendJson() so the route’s outbound policy is applied.
The simple mental model
sendJson is a careful packer: give it a JavaScript value and it turns that value into one JSON message before placing it on the socket.
When should I use it?
Use it for structured server-to-client messages instead of repeating JSON.stringify and transport checks throughout handlers.
Follow the example
This pattern explains the named API. Application classes, credentials, and assets may need to be supplied; use a complete recipe for a runnable starting point.
import { sendJson } from 'redweb'
// Route handler: respects the route's transport policy.
socket.sendJson({ type: 'ping' })
// Raw socket you own: serialization/send only.
sendJson(rawSocket, { type: 'ping' })- 1
Inside a handler, socket.sendJson uses the route’s outbound checks.
- 2
Standalone sendJson(socket, data) serializes and sends without inheriting a route policy.
- 3
A true result means the send was accepted locally, not that a peer received it.
Methods and members
sendJson(socket, data)
Serialises data and writes it to the socket.