sendJson
Utility to JSON.stringify data and send it over a `ws` socket.
Explain it like I’m five
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.
const { sendJson } = require('redweb')
sendJson(socket, { type: 'ping' })- 1
Application code creates a normal object with a type and payload.
- 2
sendJson serializes it once using the route’s safe sending path.
- 3
The client receives one complete text frame and parses the matching JSON value.
Methods and members
sendJson(socket, data)
Serialises data and writes it to the socket.