Skip to content

CallsRtc#

Beta version

The functionality is in beta mode. Features are subject to change and may also work unstably. There may be additional charges for functionality in the future.
You can request access to the functionality via GREEN-API support

callsRtc is a WebSocket connection that carries everything related to a call except commands:

  • the server reports what is happening to the call: dialing has started, an incoming call has arrived, the conversation has started, the call has ended;
  • the application and the server agree on audio transmission using WebRTC technology.

Open the connection before you call any of the calls methods and keep it open for the entire call. The callsDial, callsAccept, callsReject and callsHangUp methods return 200 OK. What happens to the call next can only be seen from the messages in this connection.

Connection#

To receive call messages, you have to establish a websocket connection at:

WSS
{{apiUrl}}/waInstance{{idInstance}}/callsRtc/{{apiTokenInstance}}

The address is the same as for the other methods, only the scheme is different: wss is specified instead of https.

For apiUrl, idInstance and apiTokenInstance websocket connection parameters, refer to Before you start section.

Note

Immediately after connecting, the server sends the current call state, and then sends it on every change. Because of this, an application restarted in the middle of a call gets the correct state without additional requests.
If the connection is lost, establish it again: the call on the server does not end. However, audio transmission stops — the server tears down the audio bridge together with the connection, so after reconnecting you have to restore the audio by sending a new audio offer.

Message format#

Messages are sent in JSON format. Each message has a type field, and the other fields depend on its value: apart from type, a message contains no more than one data field, and some messages have none at all. The sets of messages in the two directions are different — there is no single format for both sides.

Messages from the server#

Parameter Type Description
type string Message type. Takes the values state, answer, ice-candidate, error
state object Call state. Returned only when type = state. The fields are described in the Call state section
answer object The server answer to the audio offer in WebRTC format. Returned only when type = answer. Pass it to WebRTC as is, you do not need to parse the content
candidate object A network route option for audio transmission in WebRTC format (ICE candidate). Returned only when type = ice-candidate. Pass it to WebRTC as is
message string Error text. Returned only when type = error. The list of values is given in the Errors section

Messages from the application#

Parameter Type Description
type string Message type. Takes the values offer, ice-candidate, stop
offer object Audio offer in WebRTC format. Sent when type = offer. The offer is always created by the application, and the server responds to it with a message of type answer — including for an incoming call
candidate object A network route option for audio transmission in WebRTC format (ICE candidate). Sent when type = ice-candidate

A message of type stop is sent without additional fields — {"type": "stop"} — and means that the application no longer transmits audio.

Call state#

The state object describes what is currently happening to the call.

Parameter Type Description
state string Call state. Takes the values:
idle — no call
inc-call — an incoming call has arrived
out-call — an outgoing call is being dialed
on-call — the conversation is in progress
info object Call and other party data. Returned in the inc-call, out-call and on-call states
reason string Reason for ending the call. Returned only in the message about the change to the idle state and only if the server has specified it

info object#

Parameter Type Description
id string Call ID
wid string ID of the other party, for example 79001234567@c.us or 120650379300963@lid
name string Name of the other party from their WhatsApp profile. Empty if the profile has no name; for a call from a lid ID, the name is usually missing. In this case, you can find the name yourself with the GetContacts method by comparing wid with the id and newChatId fields of the contacts

Reasons for ending a call#

The value of the reason field is the server's word on why the call ended. Known values:

Value Description
hangup The call was hung up
timeout The waiting time has expired
accepted_elsewhere The call was accepted on another device of the same account, for example on the phone
rejected_elsewhere The call was rejected on another device of the same account
no-media No audio was transmitted
connect-timeout The connection could not be established within the allotted time
instance-gone The instance became unavailable
rejected:<reason> The call was rejected, the server specifies the reason after the colon

The list is open: if you receive a value that is not in the table, display it as is instead of discarding it.

Errors#

The server reports an error with a message of type error, the error text is in the message field.

{
    "type": "error",
    "message": "no active call"
}
message value Description
no active call The audio offer was sent when there is no call. First wait for the callsDial or callsAccept method to complete, and only then send a message of type offer

Apart from error messages, the server may close the connection. The close code shows whether you should reconnect:

Close code Description
4000 to 4999 The server refused: calls are not enabled on the instance or are disabled on the server side. Reconnecting will not help — eliminate the cause, and then open a new connection
Other codes Connection loss. Establish the connection again; the ongoing call is kept on the server, and the audio will have to be restored

Message examples#

Dialing of an outgoing call has started:

{
    "type": "state",
    "state": {
        "state": "out-call"
    }
}

The call has ended because the call was hung up:

{
    "type": "state",
    "state": {
        "state": "idle",
        "reason": "hangup"
    }
}

The application reports that it no longer transmits audio:

{
    "type": "stop"
}