Skip to main content

Task.Client

A Task client. On your server, use instances of this client to receive data emitted with Task.send.

Example

const client = new Task.Client({
project: "<project-id>",
token: "<api-token>",
topics: ["office"],
});

client.on("task.received", (payload) => {
console.log("Task Received", payload);
// Do something with the payload...
});

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 project token, e.g. PT9e5660c101cd140a1c93a0197640a369cf5f16975a0079c9.
opts.debug?Object-
opts.debug.logWsTraffic?booleanIf true, logs all WebSocket traffic. Default is false.

Example

const client = new Task.Client({
project: "<project-id>",
token: "<api-token>",
topics: ["office"],
});

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.

Events

task.received

task.received(payload)

Emitted whenever a task is received. Your event handler receives the payload. Example:

const client = new Task.Client(...)
client.on('task.received', (payload) => {
console.log('Task Received', payload)
// Do something with the payload...
})

Parameters

NameTypeDescription
payloadRecord<string, unknown>The message payload.