Skip to main content

Relay SDK for Node.js

Getting Started

The Relay SDK for Node.js enables Node.js developers to connect and use SignalWire's Relay APIs within their own Node.js code. Our Relay SDK allows developers to build or add robust and innovative communication services to their applications.

Installation

Install the package using NPM:

npm install @signalwire/node

Minimum Requirements

The Node.js SDK may be used with Node.js 8.0 or greater.

Using the SDK

The Node.js SDK can be used to get up and running with Relay quickly and easily. In order to use the Node.js client, you must get your project and token from your SignalWire dashboard.

There are a few ways to get started, depending on your needs: Relay.Consumer, Relay.Task, and Relay.Client.

Relay Consumer

A Relay.Consumer creates a long running process, allowing you to respond to incoming requests and events in realtime. Relay Consumers abstract all the setup of connecting to Relay and automatically dispatches workers to handle requests; so you can concentrate on writing your code without having to worry about multi-threading or blocking, everything just works. Think of Relay Consumers like a background worker system for all your calling and messaging needs.

Relay Consumers can scale easily, simply by running multiple instances of your Relay.Consumer process. Each event will only be delivered to a single consumer, so as your volume increases, just scale up! This process works well whether you are using Docker Swarm, a Procfile on Heroku, your own webserver, and most other environments.

Setting up a new consumer is the easiest way to get up and running.

const { RelayConsumer } = require('@signalwire/node')

const consumer = new RelayConsumer({
project: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
token: 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
contexts: ['home', 'office'],
ready: async ({ client }) => {
// Consumer is successfully connected with Relay.
// You can make calls or send messages here..
},
onIncomingCall: async (call) => {
const { successful } = await call.answer()
if (!successful) { return }

await call.playTTS({ text: 'Welcome to SignalWire!' })
}
})

consumer.run()

Learn more about Relay Consumers

Relay Task

A Relay.Task is simple way to send jobs to your Relay.Consumers from a short lived process, like a web framework. Relay Tasks allow you to pass commands down to your Consumers without blocking your short lived request. Think of a Relay Task as a way to queue a job for your background workers to processes asynchronously.

For example, if you wanted to make an outbound call and play a message when your user clicks a button on your web application, since Relay is a realtime protocol and relies on you to tell it what to do in realtime, if you did this within your web application, your web server would block until the call was finished... this may take a long time! Instead, simply create a new Relay Task. This task will be handled by a running Relay Consumer process and your web application can respond back to your user immediately.

Send a task in the office context with custom data.

// create-task.js
const { Task } = require('@signalwire/node')

async function main() {
const yourTask = new Task('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
const context = 'office'
const data = {
uuid: 'unique id',
data: 'data for your job'
}
try {
await yourTask.deliver(context, data)
} catch (error) {
console.log('Error creating task!', error)
}
}

main().catch(console.error)

Handle the task in a Consumer.

// consumer-task.js
const { RelayConsumer } = require('@signalwire/node')

const consumer = new RelayConsumer({
project: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
token: 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
contexts: ['office'],
onTask: (message) => {
console.log('Inbound task', message)
// Retrieve your custom properties from 'message'..
const { uuid, data } = message
}
})

consumer.run()

Learn more about Relay Tasks

Relay Client

Relay.Client is a lower level object, giving you a basic connection to Relay but that is all. It is best used when you are creating a script only concerned with sending outbound requests or you want complete control over the Relay connection yourself.

Setting up a new client and make an outbound call.

const { RelayClient } = require('@signalwire/node')
const client = new RelayClient({
project: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
token: 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
})

client.on('signalwire.ready', async (client) => {

const dialResult = await client.calling.dial({
type: 'phone',
from: '+1XXXXXXXXXX',
to: '+1YYYYYYYYYY'
})

if (dialResult.successful) {
const { call } = dialResult
// Your call has been answered..
}
})

client.connect()

Learn more about Relay Clients

Contexts

Relay uses Contexts as a simple way to separate events to specific consumers, allowing you to write consumers for specific types of calls or messages or scale them independently. A Context is simply a named string, that allows you to categorize requests. When creating outbound requests, or configuring phone numbers for inbound requests, you can specify the context; Relay will then deliver that call or event to Consumers that are configured to listen for that context.

For example, you could have a customer support phone number configured to send to Relay with the support context, and a personal number configured with personal context. Relay would deliver these events to any Consumer listening for those contexts. This gives you a lot of control in how messages are delivered to your Consumers, allowing you to write Consumer classes specific to the context, scale them independently, or separate traffic based on your own business rules.

API Reference