connect

View as Markdown

connect

  • connect(params): Promise<Call>

Attempt to connect an existing call to a new outbound call. The two devices will hear each other. You can wait until the new peer is disconnected by calling disconnected.

This is a generic method that allows you to connect to multiple devices in series, parallel, or combinations of both with the use of a DeviceBuilder.

For simpler use cases, we recommend using connectPhone or connectSip.

Parameters

params
objectRequired

Object containing the parameters for connecting the call.

devices
VoiceDeviceBuilderRequired

A device builder specifying the devices to call. See VoiceDeviceBuilder.

ringback
VoicePlaylist

Ringback audio to play to the first call leg. You can play audio, TTS, silence or ringtone. See VoicePlaylist.

maxPricePerMinute
number

The maximum price in USD acceptable for the call to be created. If the rate for the call is greater than this value, the call will not be created. If not set, all calls will be created. Price can have a maximum of four decimal places, i.e. 0.0075.

Returns

Promise<Call>

A promise that resolves to a Call object that you can use to control the new peer. The promise resolves only after the new peer picks up the call.

Examples

Connecting to a new SIP call, while playing ringback audio to the first leg of the call.

1import { SignalWire, Voice} from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const voiceClient = client.voice;
6
7const ringback = new Voice.Playlist().add(
8 Voice.Playlist.Ringtone({
9 name: "it"
10 })
11);
12
13await voiceClient.listen({
14 topics: ["office"],
15 onCallReceived: async (call) => {
16
17 let plan = new Voice.DeviceBuilder().add(
18 Voice.DeviceBuilder.Sip({
19 // Replace the to and from with valid SIP endpoint domains
20 from: `sip:${call.from}@example.sip.signalwire.com`,
21 to: "sip:example@example.sip.signalwire.com",
22 timeout: 30
23 })
24 );
25 // Answer the call
26 call.answer();
27 // Connect the call to the device builder plan
28 const peer = await call.connect({
29 devices: plan,
30 ringback: ringback
31 });
32 // Listen to peer state changes
33 await peer.listen({
34 onStateChanged: (state) => {
35 console.log("Peer state changed:", state.state);
36 }
37 })
38 // wait for peer to hangup
39 await peer.disconnected();
40 console.log("The peer hungup");
41 // hangup the call
42 call.hangup();
43 }
44});