PubSub Client

View as Markdown

The PubSub Client enables real-time publish/subscribe messaging. It connects to SignalWire and allows you to subscribe to channels and publish messages to them.

1import { PubSub } from "@signalwire/realtime-api";
2
3const pubSubClient = new PubSub.Client({
4 project: "<project-id>",
5 token: "<api-token>",
6});

After instantiating, call subscribe() to join channels, then use publish() to send messages and listen for the message event to receive them. See Events for all available events.

Constructor

new PubSub.Client(opts): PubSub.Client

Creates a new PubSub Client instance.

Parameters

NameTypeDescription
opts.projectstringRequired. SignalWire project ID.
opts.tokenstringRequired. SignalWire API token.
opts.debug.logWsTraffic?booleanLog WebSocket traffic for debugging. Default: false.

Examples

Subscribing to channels

1await pubSubClient.subscribe(["notifications"]);
2
3pubSubClient.on("message", (message) => {
4 console.log("Received:", message.content);
5});

Publishing messages

1await pubSubClient.publish({
2 channel: "notifications",
3 content: { type: "alert", text: "New update available" },
4});