Client
Constructors​
constructor​
• new Client(pubSubOptions
)
Creates a new PubSub client.
Parameters​
Name | Type | Description |
---|---|---|
pubSubOptions | Object | - |
pubSubOptions.project | string | SignalWire project id, e.g. a10d8a9f-2166-4e82-56ff-118bc3a4840f |
pubSubOptions.token | string | SignalWire API token |
pubSubOptions.debug? | Object | - |
pubSubOptions.debug.logWsTraffic? | boolean | If true , logs all WebSocket traffic. Default is false . |
Example​
import { PubSub } from "@signalwire/realtime-api";
const pubSubClient = new PubSub.Client({
project: "<project-id>",
token: "<api-token>",
});
Methods​
disconnect​
â–¸ disconnect(): 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.
Returns​
void
Example​
client.disconnect();
off​
â–¸ off(event
, fn?
)
Remove an event handler.
Parameters​
Name | Type | Description |
---|---|---|
event | string | Name of the event. See Events for the list of available events. |
fn? | Function | An event handler which had been previously attached. |
on​
â–¸ on(event
, fn
)
Attaches an event handler to the specified event.
Parameters​
Name | Type | Description |
---|---|---|
event | string | Name of the event. See Events for the list of available events. |
fn | Function | An event handler. |
Example​
In the below example, we are listening for the call.state
event and logging the current call state to the console.
This means this will be triggered every time the call state changes.
call.on("call.state", (call) => {
console.log("call state changed:", call.state);
});
once​
â–¸ once(event
, fn
)
Attaches an event handler to the specified event. The handler will fire only once.
Parameters​
Name | Type | Description |
---|---|---|
event | string | Name of the event. See Events for the list of available events. |
fn | Function | An event handler. |
publish​
â–¸ publish(params
): Promise<void>
Publish a message into the specified channel.
Parameters​
Name | Type | Description |
---|---|---|
params | Object | - |
params.channel | string | Channel in which to send the message. |
params.content | any | The message to send. This can be any JSON-serializable object. |
params.meta? | Record<any, any> | Metadata associated with the message. There are no requirements on the content of metadata. |
Returns​
Promise<void>
Examples​
Publishing a message as a string:
await pubSubClient.publish({
channel: "my-channel",
content: "Hello, world.",
});
Publishing a message as an object:
await pubSubClient.publish({
channel: "my-channel",
content: {
field_one: "value_one",
field_two: "value_two",
},
});
removeAllListeners​
â–¸ removeAllListeners(event?
)
Detaches all event listeners for the specified event.
Parameters​
Name | Type | Description |
---|---|---|
event? | string | Name of the event (leave this undefined to detach listeners for all events). See Events for the list of available events. |
subscribe​
â–¸ subscribe(channels
): Promise<void>
List of channels for which you want to receive messages.
Note that the subscribe
function is idempotent, and calling it again with a different set of channels will not unsubscribe you from the old ones. To unsubscribe, use unsubscribe.
Parameters​
Name | Type | Description |
---|---|---|
channels | string | string[] | The channels to subscribe to, either in the form of a string (for one channel) or an array of strings. |
Returns​
Promise<void>
Example​
const pubSubClient = new PubSub.Client({
project: "<project-id>",
token: "<api-token>",
});
await pubSubClient.subscribe("my-channel");
await pubSubClient.subscribe(["chan-2", "chan-3"]);
unsubscribe​
â–¸ unsubscribe(channels
): Promise<void>
List of channels from which you want to unsubscribe.
Parameters​
Name | Type | Description |
---|---|---|
channels | string | string[] | The channels to unsubscribe from, either in the form of a string (for one channel) or an array of strings. |
Returns​
Promise<void>
Example​
await pubSubClient.unsubscribe("my-channel");
await pubSubClient.unsubscribe(["chan-2", "chan-3"]);
Events​
message​
• message(message
)
A new message has been received.
Parameters​
Name | Type | Description |
---|---|---|
message | PubSubMessage | The message that has been received. |