Guide

Peer

Peer object allows easily interacting with connected clients.

Websocket hooks accept a peer instance as their first argument. You can use peer object to get information about each connected client or send a message to them.

You can safely log a peer instance to the console using console.log it will be automatically stringified with useful information including the remote address and connection status!

API

peer.send(message, compress)

Send a message to the connected client

peer.addr

The peer address (might be undefined)

peer.id

A unique id assigned to the peer.

peer.readyState

Client connection status (might be undefined)

Read more in readyState in MDN.

peer.ctx

peer.ctx is an object that holds adapter specific context. It is scoped with peer.ctx.[name].

`ctx`` is an advanced namespace and API changes might happen, so don't rely on it as much as possible!

peer.subscribe(channel)

Join a broadcast channel.

Read more in Guide > Pubsub.

peer.unsubscribe(channel)

Leave a broadcast channel.

Read more in Guide > Pubsub.

peer.publish(channel, message)

broadcast a message to the channel.

Read more in Guide > Pubsub.
Native pub/sub is currently only available for Bun and Node.js with uWebSockets.