Client
You can use instances of this class to send or receive messages.
Example
import { Messaging } from "@signalwire/realtime-api";
const client = new Messaging.Client({
project: "<project-id>",
token: "<api-token>",
topics: ["office"],
});
client.on("message.received", (message) => {
console.log("message.received", message);
});
await client.send({
topic: "office",
from: "+1xxx",
to: "+1yyy",
body: "Hello World!"
});
Constructors
constructor
• new Client(opts
)
Parameters
Name | Type | Description |
---|---|---|
opts | Object | - |
opts.topics | string[] | SignalWire topics, e.g. ['home', 'office']. Previously known as "context" . |
opts.project | string | SignalWire Project ID, e.g. a10d8a9f-2166-4e82-56ff-118bc3a4840f . |
opts.token | string | SignalWire API token, e.g. PT9e5660c101cd140a1c93a0197640a369cf5f16975a0079c9 . |
opts.debug? | Object | - |
opts.debug.logWsTraffic? | boolean | If true , logs all WebSocket traffic. Default is false . |
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. |
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. |