Skip to main content

Client

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

Task Client

Setting up a Task Client

To create a Task client, you will first need to create a SignalWire Realtime-Client. After the SignalWire Client is created, you can access the Task client using the task namespace.

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

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

const taskClient = client.task

Methods

listen

listen({ event: Callback }): Promise<TaskEvents>

Listens for incoming tasks.

Parameters

NameTypeDescription
paramsObjectObject containing the parameters of the method.
params.topicsstring[]Topics to listen to for events. Previously known as "contexts".
params.EVENTstringThe event to listen to. List of events can be found here.
Example event: (E.g: onTaskReceived)

Returns

Promise<TaskEvents>

A promise that resolves to a TaskEvents object that you can use to view the current state or results of the event.

Example

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

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

const taskClient = client.task

await taskClient.listen({
topics: ['office'],
onTaskReceived: (payload) => {
console.log('Received task', payload)
}
});

send

Const send(params): Promise<void>

Send a job to your Task Client in a specific topic.

Parameters

NameTypeDescription
paramsObject-
params.topic | params.topicsstring | string[]Topic to send the task to. Previously known as "context" | "contexts".
params.messageRecord<string, unknown>Message to send.
params.project?stringSignalWire project id, e.g. a10d8a9f-2166-4e82-56ff-118bc3a4840f.
params.token?stringSignalWire project token, e.g. PT9e5660c101...a360079c9.

Returns

Promise<void>

Example

Sending a task with a message to then make an outbound Call. Please note that the call is not performed automatically: your Task.Client gives you back your payload, which you should interpret by your own custom logic.

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

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

const clientTask = client.task

const message = {
'action': 'call',
'from': '+1XXXXXXXXXX',
'to': '+1YYYYYYYYYY'
}

const taskSend = await clientTask.send({
topic: 'office',
message: message
})

Events

onTaskReceived

client.task.listen({ onTaskReceived: Callback })

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

Parameters

NameTypeDescription
payloadRecord<string, unknown>The message payload.