Hear from Salesforce leaders on how to create and deploy Agentforce agents.
Contact Us 1-800-596-4880

WebSockets Connector Examples - Mule 4

The following Mule app examples use Anypoint Connector for WebSockets (WebSockets Connector) to build an integration system that obtains and broadcasts stock quotes.

Quote Producer App

The following Mule app generates continuous random stock quotes.

Quote Producer App flow in Anypoint Studio Canvas
  1. Stock quotes example:

{
   "ticker": "CRM",
   "price": 157.6,
   "cur": "USD",
  "timestamp": 1563374475104
}
  • The Quote type identifies the previous mentioned stock quote sample.

  • The Mule app generates approximately 50 quotes with random stock prices that uses at least 5 different tickers, such as CRM, MELI, GOOG, NFLX and APPL.

  • The app also exposes the TLS secure wss://localhost:60000/feed endpoint that accepts only one connection.

  • The first client must connect to the endpoint successfully.

  • Subsequent clients that want to connect to the endpoint receive a WSS message with the text "Sorry, spot taken".

  • Each produced quote is sent to the connected feed client.

To generate the corresponding keystores:

  1. Open your terminal.

  2. In the terminal, navigate to the src/main/resources folder of your project app in Studio.

  3. Run the following command:

    keytool -genkey -v -keystore producer-keystore.jks -alias producerkey -keyalg RSA -keysize 2048 -validity 10000

XML for the Quote Producer App

Paste this code into your Studio XML editor to quickly load the flow for this example into your Mule app:

Quote Aggregator App

The following Mule app connects to the feed endpoint of the previous Quote Producer app and receives all of the quotes. Then, the Quote Aggregator app splits the quotes by ticker and places them in time-based aggregators that occur every 5 seconds.

Quote Aggregator App flow in Anypoint Studio Canvas

When the aggregations occur, the aggregators output a reduced Array<Quote> to a single QuoteSnapshot type.

QuoteSnapshot Type example:
{
   "ticker": "CRM"
   "price": "157.54 USD"
}
json
  • The reduction occurs by picking the Quote with the greatest timestamp and transforming it.

  • The produced snapshots broadcast to a dynamic list of subscribers.

  • Subscriptions are done through the wss://localhost:8082/quotes endpoint that the Quote Aggregator App exposes.

  • Clients can connect to the previous endpoint by using a query parameter to indicate what stock quotes to follow, for example: wss://localhost:8082/quotes?ticker=CRM&ticker=MELI

To generate the corresponding keystores and truststores:

  1. Open your terminal.

  2. In the terminal, navigate to the src/main/resources folder of your project app in Studio.

  3. Run the following command for keystores:

    keytool -genkey -v -keystore broadcast-keystore.jks -alias broadcast -keyalg RSA -keysize 2048 -validity 10000

  4. Run the following command to generate the truststores:

    keytool -genkey -v -keystore aggregator-truststore.jks -alias broadcast -keyalg RSA -keysize 2048 -validity 10000

The request establishes a WebSocket that gets the snapshots for the CRM and MELI tickers. This is done should by subscribing the resulting sockets to the proper socket groups.

To connect the Quote Aggregator app with the Quote Producer app, trigger the flow to open the outbound socket with the following command:

XML for the Quote Aggregator App

Paste this code into your Studio XML editor to quickly load the flow for this example into your Mule app:

Quote Client App

The following Mule app opens at least three different WebSockets to the quotes endpoint in the Quote Aggregator app. Each of those sockets listen to a different set of tickers.

Quote Client App flow in Anypoint Studio Canvas
Figure 1. Quote Client App flow

The received QuoteSnapshots is transformed to CSV format and appended to a file.

To generate the corresponding keystores:

  1. Open your terminal.

  2. In the terminal, navigate to the src/main/resources folder of your project app in Studio.

  3. Run the following command:

    keytool -genkey -v -keystore client-truststore.jks -alias client -keyalg RSA -keysize 2048 -validity 10000

To connect the Quote Client app with the Quote Aggregator app, trigger the flow to open the outbound socket with the following command:

XML for the Quote Client App

Paste this code into your Studio XML editor to quickly load the flow for this example into your Mule app:

View on GitHub