Skip to content

Toro Cloud Dev Center


examples package: 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.

Martini 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 functions from the WebSocket class.

Connecting and disconnecting

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

1
2
3
4
var url = "/martini-ws"; // Martini'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 Martini, 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 Martini. 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 Martini via a WebSocket. The source code to these pages can be found at <martini-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.jsp page in your browser (where <host>:<port> points to the location of your deployed Martini 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 a number guessing game 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.jsp in your browser (where <host>:<port> points to the location of your deployed Martini instance).

Chat bot implementation using WebSocket