Some time ago, MuleSoft introduced Web Socket Connector. In this article, I briefly describe what web socket gives us in the context of integration. The main focus of the article is to show you how to publish a web socket for your consumers.

Web Socket

So the first question is what a web socket is. Web socket is a communication protocol over TCP that enables bidirectional communication between client and server.

web socket and HTTP

WebSocket vs HTTP communication

As you can see in the diagram above, the client initiate connection over WebSocketor secure WebSocket (**wss), **and then the server can send back messages to the client. Unline with HTTP protocol we do not need to do any pulling. Once the connection is established, the server can send as many requests as it likes.

The greatest benefit of using Web Socket instead of HTTP is performance boost as we do not need to establish a new connection. What is more, we do not need to introduce any sort of pulling mechanism.

WebSocket Connector

Exchange Rates Case

We want to consume forex exchange rates and display them on our banking portal. As they are changing rapidly, we would like to keep values up to date. I have decided to expose WebSocket endpoint to stream changes to the client.

exchange rates

MuleSoft case with published WebSocket endpoint

MuleSoft best practice is API-led connectivity. As a result, in our case, we have three layers. We have Banking Portal Experience API (XAPI), Banking Process API (PAPI) and Banking Forex System API (SAPI). In order to make the data flow smoothly, we will use Amazon SQS queues to exchanged data between layers – as in the diagram above.

Our system API reads exchange rates and publishes them to exchange-rates-sapi queue. Banking Process API reads values from this queue and saves them to exchange-rates-papi queue. This queue will be read by the application in the Experience layer. The Banking Portal XAPI broadcast exchange rates to open web sockets.

When the client initiates a web socket connection, it must decide the primary currency. In other words, if exchange rates should be calculated against USD, EUR, PLN currencies.

WebSockets Configuration

WebSocket configuration requires **HTTP Listener **config. In the picture below, you can see that we have to assign existing HTTP_Listener_config to WebSocket configuration by **Listener config **property.

http_listener

WebSocket global configuration

You can specify how long the connection should be kept while idle. In my scenario, I have decided to kill the connection after 60 minutes.

WebSocket Connector Operations

In order to expose WebSocket connection we have two listeners.

**On New Inbound Connection **is triggered when the client initiates the connection. Below you can see sample JavaScrip line that performs this action. In this case, you don’t have any payload, but you receive the headers/metadata.

Java

1

var ws = new WebSocket("ws://localhost:8081/ws/exchange-rates");

On New Inbound Message is triggered when the client sends the message on already establish WebSocket connection. The client can send a body that Mule saves within the payload – like JSON. In this part, we often subscribe to some events like chat entries entered by all the users or exchange rate changes. We can also send a message back to the client if we like.

In the JavsScript code snippet below, you can find sending the message to WebSocket.

#tutorial #integration #api #mulesoft #http #websocket

Publish Web Socket in the Experience Layer
3.00 GEEK