Skip to main content

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

NameTypeDescription
optsObject-
opts.topicsstring[]SignalWire topics, e.g. ['home', 'office']. Previously known as "context".
opts.projectstringSignalWire Project ID, e.g. a10d8a9f-2166-4e82-56ff-118bc3a4840f.
opts.tokenstringSignalWire API token, e.g. PT9e5660c101cd140a1c93a0197640a369cf5f16975a0079c9.
opts.debug?Object-
opts.debug.logWsTraffic?booleanIf 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

NameTypeDescription
eventstringName of the event. See Events for the list of available events.
fn?FunctionAn event handler which had been previously attached.

on

on(event, fn)

Attaches an event handler to the specified event.

Parameters

NameTypeDescription
eventstringName of the event. See Events for the list of available events.
fnFunctionAn event handler.

once

once(event, fn)

Attaches an event handler to the specified event. The handler will fire only once.

Parameters

NameTypeDescription
eventstringName of the event. See Events for the list of available events.
fnFunctionAn event handler.

removeAllListeners

removeAllListeners(event?)

Detaches all event listeners for the specified event.

Parameters

NameTypeDescription
event?stringName of the event (leave this undefined to detach listeners for all events). See Events for the list of available events.

send

send(params): Promise<MessagingSendResult> - See MessagingSendResult

Send an outbound SMS or MMS message.

Parameters

NameTypeDescription
paramsObject-
params.body?stringThe content of the message. Optional if media is present.
params.topic?stringInbound events for the message will be received on this topic. If not specified, a default topic will be used. Previously known as "context".
params.fromstringThe phone number to place the message from. Must be a SignalWire phone number or short code that you own.
params.media?string[]Array of URLs to send in the message. Optional if body is present.
params.region?stringRegion of the world to originate the message from. A default value is picked based on account preferences or device location.
params.tags?string[]Array of strings to tag the message with for searching in the UI.
params.tostringThe phone number to send to.

Returns

Promise<MessagingSendResult> - See MessagingSendResult

Asynchronously returns a result object.

Example

Send a message.

try {
const sendResult = await client.send({
from: "+1xxx",
to: "+1yyy",
body: "Hello World!",
});
console.log("Message ID: ", sendResult.messageId);
} catch (e) {
console.error(e.message);
}

Events

message.received

message.received(message)

Emitted whenever a message is received. Your event handler receives a message object. Example:

const client = new Messaging.Client(...)
client.on('message.received', (message) => {
console.log('Message received:', message)
// message.from
// message.to
// message.body
// ...
})

Parameters

NameTypeDescription
messageMessageContractThe message that has been received.

message.updated

message.updated(message)

Emitted when the status of a message is updated. You can use this event to track the different stages that an outbound message goes through for delivery. Example:

const client = new Messaging.Client(...)
client.on('message.updated', (message) => {
console.log('Message updated:', message)
// message.from
// message.to
// message.direction
// message.state
// ...
})

client.send(...)

Parameters

NameTypeDescription
messageMessageContractThe message.