how to create a websocket client in mule

206 views Asked by At

I'm trying to create a websocket client in mule which connect to external server websocket. Also I want to send the headers and body to external websocket at the time of connection request. How to achieve this in mule ?

1

There are 1 answers

2
aled On BEST ANSWER

How to send a WebSockets request and set headers is explained in the Mule WebSockets connector documentation. To send information you can use the send operation.

Example

<websocket:config name="wsClient">
    <websocket:connection>
       <websocket:client-settings host="localhost" port="${listenerPort}" basePath="/ws">
           <websocket:default-headers>
                <websocket:header key="myFirstDefaultHeader" value="defaultHeader1" />
                <websocket:header key="mySecondDefaultHeader" value="defaultHeader2" />
            </websocket:default-headers>
            <websocket:default-query-params>
                <websocket:query-param key="myFirstDefaultQueryParam" value="query1" />
                <websocket:query-param key="mySecondDefaultQueryParam" value="query2" />
            </websocket:default-query-params>
       </websocket:client-settings>
    </websocket:connection>
</websocket:config>

<flow="connectToWebSocketFlow">
    <websocket:open-outbound-socket
      path="/chat"
      socketId="myCustomWebSocketID-123"
      config-ref="wsClient" />
    </websocket:open-outbound-socket>

    <logger info="INFO" message="Opened connection to external service!"/>

     <websocket:send socketId="myCustomWebSocketID-123" config-ref="wsClient">
        <websocket:content>'Hello world!'</websocket:content>
     </websocket:send>
</flow>