Step by Step tutorial for using WebSockets connector in Mule 4

How to use WebSockets connector in Mule 4 for streaming?



We can use the WebSockets connector to develop a streaming API in MuleSoft either from another WebSocket or an HTTP Request configuration. To stream the data from a WebSocket we would need existing WebSocket connection details. If we want to make an HTTP call, we can use the Scheduler component to poll for data.

Here are the patterns which are highly used
  1. To stream all the data from WebSockets

  2. To stream all the data to the clients from an HTTP request

  3. To stream only data which is requested by the client

This article covers the steps for developing pattern 1 which is streaming the data from an existing WebSocket

Streaming the data from a WebSocket in MuleSoft



1. You can use the below URL of an existing open-source WebSocket. You do not need an API key to connect with the below WebSocket.

CoinCap WebSocket: wss://ws.coincap.io/trades/binance

2. Installation of WebSockets connector from Exchange. It is recommended to use Mule 4.3 Runtime



3a. Add the On New Inbound Message as Listener configuration. This will be the endpoint configuration that is shared with the end-users

3b. Click on Add button in Connector configuration. Configure WebSockets server settings


3c. Configure the path


The final endpoint would look like ws://localhost:8082/ws/wsdata

4a. Now, let us configure the WebSockets outbound configuration. Refer to ExternalWebSocket flow from the above flows screenshot. Add Open Outbound Socket from the Palette.

4b. Configure the client settings. Select Connection [1] and Client settings [2] as below
 

4c. Add the Path as highlighted below and you are done with the Outbound Socket configuration



5a. Configuring the Broadcast component. Refer BroadcastWebSocket flow from the above



5b) Configure the Broadcast component as below. Select the [1] same configuration used in Step 3b. Specify the content to be streamed [2].

Specify the basePath and Path same as above Step 3b. ws/wsdata


Testing Websockets using the below methods

  1. Install Smart Websocket Client or

  2. Open https://postwoman.io







Find the latest MuleSoft(Mule 4) Articles on DataWeave, Anypoint Studio, Anypoint Platform, and Runtime Manager. Our articles also include frequently asked Mule4 Interview questions and answers. Please comment below for any queries or free article submissions.

Post a Comment

0 Comments