sendJson

Documentation for Redweb 0.16.1. Install that exact version when following these examples.

Low-level JSON serialization/send helper. Inside a route, prefer socket.sendJson() so the route’s outbound policy is applied.

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.

js
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.

What should I watch for?

The standalone helper can throw on serialization or transport failure. It does not automatically apply route backpressure. Bound payloads and use the route-bound helper when available.