Utility · In depth

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 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
const { sendJson } = require('redweb')

sendJson(socket, { type: 'ping' })
  1. 1

    Application code creates a normal object with a type and payload.

  2. 2

    sendJson serializes it once using the route’s safe sending path.

  3. 3

    The client receives one complete text frame and parses the matching JSON value.

Surface area

Methods and members

sendJson(socket, data)

Serialises data and writes it to the socket.