Contact Free trial Login

WebSockets Messaging Operations - Mule 4

Send Operation

The Send operation allows you to send a message through a WebSocket identified by its socket ID after a connection is established. This operation can be used for both inbound and outbound WebSocket instances.

    <flow name="sayHello">
      <websocket:open-outbound-socket
        path="/chat/hello"
        config-ref="ws"
        target="socketId"
        targetValue="#[attributes.socketId]"/>
      <websocket:send socketId="#[vars.socketId]" config-ref="ws">
        <websocket:content>'Good morning!'</websocket:content>
      </websocket:send>
    </flow>

    <flow name="sayHelloBack">
        <websocket:inbound-listener
            path="/chat/hello"
            config-ref="ws"
            outputMimeType="text/plain" />
        <websocket:send socketId="#[attributes.socketId]" config-ref="ws">
            <websocket:content>#['Good morning ' ++ attributes.socketId ++ '!']</websocket:content>
        </websocket:send>
    </flow>

Broadcast Operation

The Broadcast operation sends a message to a group of sockets. To select the group of sockets to which the message can be broadcast:

  • Subscribe to specific groups

  • Connect to a specific path

  • Be of a specific type (INBOUND or OUTBOUND)

The type and group parameters work in tandem, in an additive way. They are both optional and can be used separately, but when used together they act as two levels of filtering:

<!-- Send to all INBOUND sockets connected to the /quotes endpoint, in the MARVEL and DC groups -->
<websocket:broadcast
    doc:name="Broadcast"
    socketType="INBOUND"
    config-ref="WebSockets_Config"
    path="/quotes">
    <websocket:groups >
        <websocket:group value=" #[['MARVEL', 'DC']]" />
    </websocket:groups>
</websocket:broadcast>

Output

The output of the Broadcast operation is void.