Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

CTE-4767 WebSockets in Canvas
Canvas offers WebSockets to achieve full-duplex communication channels over a single TCP connection. This helps you build communication protocol for a persistent, bi-directional, full duplex TCP connection from a user's web browser to a server. The Canvas WebSocket connection is initiated by sending a WebSocket handshake request from a browser's HTTP connection to a server to upgrade the connection.
In the scenario of updating a UI component of a website at frequent intervals (e.g. stock rates or train details), the WebSockets serve as optimal solution where multiple calls to the server can be made by establishing a single connection. This decreases the serving time of the requests.
Steps to Publish and Subscribe to WebSockets:

  1. Enable WEB_SOCKET_ENABLED=Y property in the securityconfig property file.
  2. Create instance for WebSocketManager in your application class file.

WebSocketManager webSocketManager=WebSocketManager.getInstance();

  1. Invoke the publishMessage method to publish the WebSocket from server to client.

...

  1. Subscribe with WebSocket channel. For the first time, the connection will be established with namespace ct.websocket.message.received.

ct.MessageBus.subscribe('subscribename','ct.websocket.message.received');
// subscribename => subscriber name
// ct.websocket.message.received => namespace and it will be always same
Sample: The following snippet subscribe establish & fetch data from web socket connection.
ct.comm.MessageBus.subscribe('monitor','ct.websocket.message.received',this,function(data){//business logic need to be done herealert('WELCOME TO WEBSOCKET'+data);});For more information referĀ Web Sockets in Canvas.