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
The event to listen to. List of events can be found here. Example event: 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
Topic to send the task to. Previously known as "context" or "contexts". Can also use params.topics.
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.