Realtime Client
The Realtime Client is the main entry point for the Realtime SDK. It provides methods to connect to the Realtime service, authenticate, and subscribe to events on a specified namespace.
Constructor
▸ new Client(opts
): Promise<SWClient>
Create a new Client instance.
Parameters
Name | Type | Description |
---|---|---|
opts | Object | - |
opts.project | string | SignalWire project id, e.g. a10d8a9f-2166-4e82-56ff-118bc3a4840f |
opts.token | string | SignalWire project token, e.g. PT9e5660c101cd140a1c93a0197640a369cf5f16975a0079c9 |
opts.debug? | Object | - |
opts.debug.logWsTraffic? | boolean | If true , logs all WebSocket traffic. Default is false . |
Example
import { SignalWire } from "@signalwire/realtime-api";
const client = await SignalWire({ project: "ProjectID Here", token: "Token Here"})
Methods
connect
▸ connect(): Promise<void>
The client will automatically connect when it is created. You only need to call this method if you have previously disconnected
.
Connects this client to the SignalWire RealTime API. The client will start receiving events.
Example
import { SignalWire } from "@signalwire/realtime-api";
const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
await client.connect().then(() => {
console.log("connected");
});
disconnect
▸ disconnect(): Promise<void>
Disconnects this client. The client will stop receiving events and you will need to create a new instance if you want to use it again.
Example
import { SignalWire } from "@signalwire/realtime-api";
const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
await client.disconnect().then(() => {
console.log("disconnected");
});
Namespace Clients
The Realtime Client offers functionalities to establish and control namespace clients. A namespace client is a type of client that is linked to a particular namespace, capable of subscribing to events within that namespace and utilizing the methods that the namespace offers.
Below is an example on how you can create a client for the Voice
, Video
, Messaging
, Chat
, Task
, and PubSub
namespaces.
import { SignalWire } from "@signalwire/realtime-api";
const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
const voiceClient = client.voice;
const videoClient = client.video;
const messagingClient = client.messaging;
const chatClient = client.chat;
const taskClient = client.task;
const pubsubClient = client.pubSub;
List of available namespace clients: