Websocket API
Note: this page describes how to build applications using websockets. If you prefer to use the webhooks API, please visit this page.
TLDR;
- Use
npx create-jambonz-ws-app
to scaffold a webhook application - See @jambonz/node-client-ws for Node.js API
The websocket API is functionally equivalent to the Webhook API; it is simply an alternative way for an application to interact with and drive jambonz call and message processing. We recommend using the websocket API for highly asynchronous applications.
When you create a jambonz application in the jambonz portal and you want to use the websocket API, simply provide a ws(s) URL for the calling webhook instead of an http(s) URL. The call status webhook can be the same ws(s) URL, in which case your application will get the call status notifications over the same websocket connections.
You can also have call status notifications sent to a completely separate http(s) webhook URL if you prefer.
The impact of specifying a ws(s) URL as the application calling webhook is that this causes jambonz to establish a websocket connection to that URL when an incoming call (or outbound call) is routed to the jambonz application, and then communicate with your application over that websocket connection.
In the documentation below, we refer to the websocket server as the “application”.
Connection management
The websocket connection will be established by jambonz to the specified websocket URL, The websocket subprotocol used shall be “ws.jambonz.org”. If jambonz fails to connect to the provided URL, there will be no retry and the call shall be rejected.
Once connected, jambonz will send an initial JSON text message to the your application with the same parameters as are provided in the webhook call. The full message set is described below, but for now we can simply say that:
- Only text frames are ever sent over the websocket connections; i.e. no binary frames.
- All text frames contain JSON-formatted data.
- The information content sent from jambonz to the your application is exactly the same content as that supplied via http webhooks.
The websocket should generally be closed only from the jambonz side, which happens when the call is ended. If the your application closes the socket, jambonz will attempt to reconnect, up to a configurable number of reconnection attempts. Upon reconnecting, jambonz will send an initial reconnect message containing only the callSid of the session. It is up to the your application to maintain the state of the application between reconnections for the same call.
Message format
As mentioned above, all messages will be JSON payloads sent as text frames. The following top-level properties will be commonly included:
- type: all messages must have a type property.
- Messages from jambonz to the your application will have the following types: [
session:new
,session:reconnect
,verb:hook
,call:status
,error
]. - Messages from the your application to jambonz will have the following types: [
ack
,command
].
- Messages from jambonz to the your application will have the following types: [
- msgid: every message sent from jambonz will include a unique message identifier. Messages from the your application application that are responses to jambonz messages (
ack
) must include the msgId that they are acknowledging.
Note that not all messages sent by jambonz need to be acknowledged. The message types which must be acknowledged are the session:new
, and verb:hook
messages.
Message types
In the sections that follow, we will describe each of the message types in detail. The tables below provides summary information for:
- client messages (sent from jambonz to your application) and,
- server messages (sent from your application to jambonz).
Client messages
The following messages are sent by jambonz to your application
Server messages
The following messages can be sent from your application back to jambonz