sendJson
Documentation for Redweb 0.13.2. Install that exact version when following these examples.
Utility to JSON.stringify data and send it over a ws socket.
Explain it like I’m five
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 API pattern illustrates the named surface; it may require application-owned classes, credentials, or assets. Start from a complete recipe for a runnable application.
const { sendJson } = require('redweb')
sendJson(socket, { type: 'ping' })- Application code creates a normal object with a type and payload.
- sendJson serializes it once using the route’s safe sending path.
- 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.
What should I watch for?
Serialization can fail on cycles and BigInt values, and large objects still consume memory. Bound payload sizes and keep messages purpose-specific.