Utility · In depth

sendJson

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

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 it fits

When should I use it?

Use it for structured server-to-client messages instead of repeating JSON.stringify and transport checks throughout handlers.

A practical API pattern

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.

Read this article as Markdown

JavaScript
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. 1

    Inside a handler, socket.sendJson uses the route’s outbound checks.

  2. 2

    Standalone sendJson(socket, data) serializes and sends without inheriting a route policy.

  3. 3

    A true result means the send was accepted locally, not that a peer received it.

Surface area

Methods and members

sendJson(socket, data)

Serialises data and writes it to the socket.