The WebSocket permits developers to open a persistent connection and send data through it. To use the module, we have to mount it at the very beginning of the scenario:
The createWebSocket method specifies URL and protocols (optional). You’ll learn how to obtain the URL to the WebSocket from the next section.
After a WebSocket object has been created, we continue to manage the call inside a handler. In short, we send the call media to the WebSocket object using the call.sendMediaTo method.
We call this method as we get the WebSocketEvents.MESSAGE event that the connection is up. In our scenario, this code will look like this:
All the other WebSocket events that you see in the code are for debugging purposes. The appropriate handlers do nothing but write info to a session log. You are free to get rid of them if you want to.
Finally, we should add a proper handler for stream ending. In this case, the Voximplant session will terminate on the Disconnected event in 1 second after an established call terminates:
When the scenario logic is clear, we’re ready to move to the next, very important part of our sample.