Skip to main content

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

SignalWire(options): Promise<SWClient>

Create a new Client instance.

Parameters

options
objectrequired

Configuration options for the client.

options.project
stringrequired

SignalWire project id, e.g. a10d8a9f-2166-4e82-56ff-118bc3a4840f

options.token
stringrequired

SignalWire project token, e.g. PT9e5660c101cd140a1c93a0197640a369cf5f16975a0079c9

options.host
string

SignalWire host URL. Default is the standard SignalWire host.

options.logLevel
string

Log level: 'trace', 'debug', 'info', 'warn', 'error', or 'silent'.

options.debug
object

Debug configuration object.

debug.logWsTraffic
boolean
Default: false

If true, logs all WebSocket traffic.

Example

import { SignalWire } from "@signalwire/realtime-api";

const client = await SignalWire({ project: "ProjectID Here", token: "Token Here"})

Methods

connect

connect(): Promise<void>

note

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: