Broadcasting Messages

Sample Number





This sample demonstrates how to broadcast messages to WebSocket clients periodically.

Use Case

I want to broadcast a message to all the WebSocket clients connected to a particular subscriber path periodically.


As shown in the above diagram, UltraESB broadcast a message periodically to all the WebSocket clients who are connected to a particular subscriber path.

What is a Subscriber path of a WebSocket Client?

Subscriber path of a WebSocket can be described as the resource path of the URL a particular WebSocket client has connected to. As an example, assume a particular WebSocket client has connected to 'ws://localhost:8887/helloworld' URL. Then the subscriber path of that WebSocket client would be 'helloworld'.

Sample Configuration

The configuration for this use case consists of a proxy service, exposed on the WebSocket transport and TimerTaskListener. The logic within the inSequence will get executed after 5 seconds have passed since the initialization of the proxy service and keeps on getting executed once every 2 seconds.

Proxy service configuration

 1<u:proxy id="stockprice-broadcaster" >
 2    <u:transport id="ws-listener">
 3        <u:property name="" value="stockprice"/>
 4    </u:transport>
 5    <u:transport id="timer-task-listener">
 6        <u:property name="ultra.polling.start_delay" value="5000"/>
 7        <u:property name="ultra.polling.repeat_interval" value="2000"/>
 8    </u:transport>
 9    <u:target>
10        <u:inSequence>
11            <u:class name=""/>
12        </u:inSequence>
13    </u:target>
16<u:endpoint id="ws-endpoint">
17    <u:address>ws://broadcast/subscribers?subscribe_path=stockprice</u:address>

The inSequnce consists of belows Java class. The content within the execute() method gets executed once every 2 seconds and within that method a sample message will be created and submitted to the endpoint 'ws-endpoint'.

The 'ws-endpoint' has the address 'ws://broadcast/subscribers?subscribe_path=stockprice' which means that any message passed to this endpoint will be broadcasted to all the WebSocket clients connected to the 'stockprice' subscribed path. i.e. all the WebSocket clients connected to 'ws://localhost:8887/stockprice' will receive this message. For more information on WebSocket addresses please refer WebSocket Transport Guide.

Content of the StockBroadcaster java class

 1public class StockBroadcaster extends AbstractJavaSequence {
 2    private int stockPrice = 8;
 3    @Override
 4    public void execute(Message msg, Mediation mediation) throws Exception {
 5        WebSocketMessage payload = null;
 6        stockPrice += 2;
 7        try {
 8            payload = new WebSocketMessage("ADRT : " + stockPrice + "USD");
 9        } catch (UnsupportedEncodingException ignore) {
10        }
11        msg.setCurrentPayload(payload);
12        mediation.sendToEndpoint(msg, "ws-endpoint");
13    }
For the simplicity the complete configuration is not displayed here, you may look at the complete configuration either from the binary distribution under samples/conf/ultra-sample-452.xml, or from the sample 452 configuration of the source tag.

In Action

To run the example, start the UltraESB sample configuration 452 via the ToolBox or on the command line as follows.

Running the sample from startup script

$ cd /opt/ultraesb-2.6.1/bin
$ ./ -sample 452

Now open the websocket-client.html client at samples/resources/websocket/ and connect to ws://localhost:8887/stockprice URL.

Browser Support
browser You must open websocket-client.html file using a browser which has native WebSocket support such as Internet Explorer 10+, Mozilla Firefox 11+, Google Chrome 16+, Opera 12.10+ or Safari 6+.

Now as you can see in the below image, you’ll receive a message to your WebSocket client periodically.

In this topic
In this topic
Contact Us