Skip to content

Overview

Development

Overview

IDEs

API Explorer

WebSockets

The WebSocket protocol enables bidirectional communication between a web client (e.g. web browser) and a web server over a single TCP connection. WebSockets are a fitting alternative to traditional HTTP communication, especially for applications that require real-time and long-lived exchange of information, boasting low latency and overhead. Because of their nature, they are typically used in chat applications, collaborative editing, and multi-player games.

TORO Integrate supports the use of WebSockets and can be utilized as a means to interact with client applications. In the following sections, we will explore how to work with WebSockets using JavaScript and take a look at one-liners from the WebSocketMethods class.

Connecting and Disconnecting

TORO Integrate exposes a Simple Text Oriented Messaging Protocol (STOMP) endpoint to allow clients to easily connect via WebSocket. For this tutorial, we will use SockJS as a client to ensure compatibility for modern browsers and environments but the WebSocket API can also be used if preferred. The following code block shows how to establish a connection to TORO Integrate:

1
2
3
4
var url = "/esbws"; // TORO Integrate's STOMP endpoint
var sockjs = new SockJS(url);
var stompClient = Stomp.over(ws);
stompClient.connect( {}, function(frame){})

To close the connection, call stompClient.disconnect().

Subscribing and Unsubscribing

After connecting to TORO Integrate, data can now be retrieved by subscribing to a destination. The following code block subscribes to the /topic/chat-room destination.

1
2
3
var subscription = stompClient.subscribe('/topic/chat-room', function (data) {
    // body can be retrieved by data.body
});

By subscribing to /topic/chat-room, you should be able to receive all messages published to this destination. To unsubscribe, call subscription.unsubscribe().

Publishing Messages

Messages can also be published to a destination after establishing a connection to TORO Integrate. The following code block sends the text "hello" to the /topic/chat-room destination.

1
stompClient.send('/topic/chat-room', {}, 'hello');

Examples

The examples package contains web pages that demonstrates how a client can communicate to TORO Integrate via WebSocket. The source code to these pages can be found at <toro-integrate-home>/packages/examples/web/websocket.

Chat Room

Among the examples provided in the examples package is a simple chat application that lets you communicate with other clients in real-time. You can access this application by visiting the <host>:<port>/examples/websocket/chat.html page in your browser (where <host>:<port> points to the location of your deployed TORO Integrate instance).

Chat room implementation using WebSocket

Number Guessing Game with Togo

Another example included in the examples package is a chat bot application that lets you play with Togo. The goal is to guess the number selected by Togo whose value is between 0 to 10. This can be accessed by visiting the page <host>:<port>/examples/websocket/index.html in your browser (where <host>:<port> points to the location of your deployed TORO Integrate instance).

Chat bot implementation using WebSocket