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.
Create a client
To get started, you need to create a new instance of the Realtime Client.
You can do this by calling the SignalWire
function and passing your Project ID
and API token
as parameters.
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: