The Call Message Events feature is in Public Beta.
Voice SDK Call Message Events Feature Overview
The Call Message Events feature allows your client-side and server-side applications to communicate via custom ("user-defined") messages during a Voice SDK call. The feature leverages the existing signaling connection, so you don't need to set up your own communication channel between your client-side and server-side applications.
Two Twilio REST API Resources are used for server-side implementation:
The
UserDefinedMessage Resource
, which allows your server-side application to send messages to the Voice SDK end user during an active Call
A Call is active and ready for sending and receiving messages when the Call.State is State.CONNECTED or State.RINGING.
The Call's state is returned via the Call.getState() method.
The Call.Listener.onConnected(...) callback is emitted when the Call state transitions to State.CONNECTED.
The Call.Listener.onRinging(...) callback is emitted when the Call state transitions to State.RINGING.
The Call is active and ready for sending and receiving messages when the Call.State is Call.State.Connected or Call.State.Ringing.
The Call's state is returned via the Call.getState() method.
The Call event Call.Event.Connected is emitted when the Call state transitions to Call.State.Connected.
The Call event Call.Event.Ringing is emitted when the Call state transitions to Call.State.Ringing.
Server side
From the server side perspective, an "active" Call is a Call Resource with a CallStatus value of either in-progress or ringing.
The status for a Call Resource can be retrieved from the body of status callback requests or by fetching a Call Resource via API.
Send messages from server, receive messages in the SDK
The general flow of sending a message from the server side to the SDK is as follows:
An SDK end user answers or places a Call and the Call is currently active.
The server-side application makes a
POST
request to the Call's
UserDefinedMessages endpoint
. This request contains the message to be sent.
The SDK receives the message.
Required setup for server to SDK messaging
Prepare your server-side application to send messages to the SDK
You must have some way of retrieving the Call SID on your server side. One way to do this is with the statusCallback and statusCallbackEvent attributes in your <Client> and <Number> TwiML, in conjunction with an endpoint that handles status callback requests from Twilio.
Prepare your client-side application to receive messages
Add logic to your client-side application that handles incoming messages.
Select your Voice SDK language/platform below to see an example of receiving messages in the SDK.
JavaScript SDKiOS SDKAndroid SDKReact Native SDK
_10
call.on("messageReceived", (message) => {
_10
console.log(JSON.stringify(message.content));
_10
//the voiceEventSid can be used for tracking the message
//the voiceEventSid can be used for tracking the message
_10
console.log('voiceEventSid: ', message.getSid());
_10
});
Send a message from the server to the SDK
Once a Call is active, send a message by sending a POST request to the Call's UserDefinedMessages endpoint. The message content is passed in the Content parameter as a JSON string.
Send a message to the SDK
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl
_20
// Download the helper library from https://www.twilio.com/docs/node/install
_20
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_20
_20
// Find your Account SID and Auth Token at twilio.com/console
_20
// and set the environment variables. See http://twil.io/secure
Provided that the message was sent successfully from the server side, the SDK will receive the message. See the code samples above for SDK-specific handling of incoming messages.
Send messages from the SDK to the server side
In order to receive messages on your server from the SDK, you need to set up a subscription to the Call's messages. In the subscription, you specify where Twilio should send the messages. Once a subscription is created, the SDK end-user can send messages that Twilio will then send in an HTTP request to your server-side application.
The general flow of sending a message from the SDK to the server side is as follows:
An SDK end user answers or places a Call and the Call is currently active.
The
call.sendMessage()
method is invoked in the SDK.
The endpoint specified in the UserDefinedMessageSubscription request receives the message from Twilio.
Required setup for SDK to server messaging
Prepare your server-side application to receive messages from the SDK
Before you can receive any messages from the SDK, you need to set up an HTTP endpoint where Twilio will send messages. Your endpoint must be able to accept application/json. This endpoint's URL is used as the
Callback
parameter when subscribing to a Call's messages.
You must have some way of retrieving the Call SID on your server side. One way to do this is with the
statusCallback
and
statusCallbackEvent
attributes in your
<Client>
and
<Number>
TwiML, in conjunction with an endpoint that handles status callback requests from Twilio.
Prepare your client-side application to send messages
Add logic to your client-side application that:
constructs a valid message object
invokes the
call.sendMessage()
method during an active call
handles the success/failure of a message sending attempt
Select your Voice SDK language/platform below to see an example of sending a message from the SDK to the server side.
JavaScript SDKiOS SDKAndroid SDKReact Native SDK
_25
// Errors related to Call Message Events are emitted by the Device instance.
_25
device.on("error", function (twilioError) {
_25
console.error(twilioError);
_25
});
_25
_25
// a Call is active
_25
_25
// add listener for 'messageSent' event
_25
call.on("messageSent", () => {
_25
console.log("Message sent.")
_25
});
_25
_25
// create the Call Message
_25
const callMessage = {
_25
content: { key1: 'This is a messsage from the parent call' },
_25
messageType: 'user-defined-message',
_25
contentType: "application/json"
_25
}
_25
_25
// send the message
_25
// the voiceEventSid can be used for tracking the message
You can only send messages during an active Call. If you have any UI elements that your SDK end user interacts with (e.g. a "Send Message" button), make sure that sending messages is only enabled during an active Call.
Subscribe to an active Call's messages
Once a Call is active, your server-side must set up a subscription to a Call's messages by making a POST request to the Call's UserDefinedMessageSubscription Resource.
The Callback parameter specifies your endpoint that will receive messages from Twilio.
Subscribe to a Call's Messages
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl
_22
// Download the helper library from https://www.twilio.com/docs/node/install
_22
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_22
_22
// Find your Account SID and Auth Token at twilio.com/console
_22
// and set the environment variables. See http://twil.io/secure
Once a subscription has been set up, the SDK can now invoke the call.sendMessage() event.
Receive the message on the server side
If a subscription was created and then the SDK sent a message successfully, your Callback endpoint will receive the request from Twilio. The message from the SDK is contained in the Content property of the request.
See an example of the Twilio's request to the Callback endpoint below, followed by a description of the parameters in the request.
_10
{
_10
ContentType: 'application/json',
_10
Content: '{"key1":"This is a messsage from the SDK"}',
_10
SequenceNumber: '1',
_10
CallSid: 'CA0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
_10
Timestamp: 'Fri, 2 Dec 2022 22:02:49 +0000',
_10
AccountSid: 'ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
_10
Sid: 'KXaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
_10
}
Parameter
Description
ContentType
The Content-Type of the request. (Currently, Twilio only supports application/json.)
Content
The message sent from the SDK as a JSON string.
SequenceNumber
The order in which the messages were sent, starting from 0.
CallSid
The SID of the Call Resource this message is associated with
Timestamp
The timestamp when Twilio sent this message, given as UTC in RFC 2822 format.
AccountSid
The Twilio Account SID associated with the message.
Sid
A unique identifier for this message. This can be used for internal logging/tracking.