Chat Client

View as Markdown

The Chat Client enables real-time messaging between users. It connects to SignalWire and allows you to subscribe to channels, publish messages, and track channel members.

1import { Chat } from "@signalwire/realtime-api";
2
3const chatClient = new Chat.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 Chat.Client(opts): Chat.Client

Creates a new Chat 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 chatClient.subscribe(["general", "support"]);
2
3chatClient.on("message", (message) => {
4 console.log(`${message.channel}: ${message.content}`);
5});

Publishing messages

1await chatClient.publish({
2 channel: "general",
3 content: "Hello, world!",
4});