receive

View as MarkdownOpen in Claude

Subscribe to additional contexts for inbound call and message events. Sends a signalwire.receive request to start receiving events on the specified contexts without reconnecting.

Use this to dynamically expand the set of contexts after the initial connection. Contexts passed to the RelayClient constructor are subscribed automatically at connect time — this method is for adding more at runtime.

Contexts are strings that act as routing labels. A phone number configured in your SignalWire dashboard sends calls to a specific context. Subscribe to that context to receive those calls.

Parameters

contexts
string[]Required

List of context names to subscribe to. If the list is empty, the method returns immediately without sending a request.

Returns

Promise<void>

Examples

Add contexts at runtime

1import { RelayClient } from '@signalwire/sdk';
2
3const client = new RelayClient({
4 project: process.env.SIGNALWIRE_PROJECT_ID!,
5 token: process.env.SIGNALWIRE_TOKEN!,
6 contexts: ['sales']
7});
8
9await client.connect();
10
11// Dynamically subscribe to additional contexts after connecting
12await client.receive(['support', 'billing']);
13console.log('Now receiving calls on sales, support, and billing');
14
15await client.disconnect();

With inbound call handler

1import { RelayClient } from '@signalwire/sdk';
2
3const client = new RelayClient({
4 project: process.env.SIGNALWIRE_PROJECT_ID!,
5 token: process.env.SIGNALWIRE_TOKEN!,
6 contexts: ['sales']
7});
8
9client.onCall(async (call) => {
10 console.log(`Call on context: ${call.context}`);
11 await call.answer();
12 await call.hangup();
13});
14
15await client.connect();
16await client.receive(['support', 'billing']);
17console.log('Now also receiving on support and billing');