October 2018
Intermediate to advanced
590 pages
15h 5m
English
Under the hood, the SockJS client will try to use the native WebSocket that the browser provides. If it is not available, it will fall back to other transport protocols, such as XHR-Streaming and XHR-Polling. Its API is very simple to use. The following is an example of establishing a WebSocket connection with a local server at the /rt path:
let socket = new SockJS('http://localhost:8080/rt')socket.onopen = function (event) { // Connection established console.log(socket.readyState)}socket.onmessage = function (message) { // Message received via WebSocket}socket.onclose = function (event) { // Connection closed}socket.onerror = function (error) { // An error occurred}
As you can see, it has four event handlers:
Read now
Unlock full access