Contact Us 1-800-596-4880

WebSockets Connector - Mule 4

WebSockets Connector Version 1.0

Anypoint Connector for WebSockets (WebSockets Connector) includes these integration capabilities:

  • Establishes bidirectional and full-duplex communication between a server and client

  • Implements server push notifications

  • Seamlessly works on top of an existing HTTP infrastructure

  • Avoids the need for clients to regularly poll the server for new data

A WebSocket is a bidirectional, TCP connection between a client and server that is kept open until the app closes it. With a WebSocket, an app uses HTTP to make an initial connection. The connection then gets upgraded to a TCP socket-based connection.

Advantages of Full-Duplex Communication

In traditional HTTP, a connection is half-duplex (unidirectional) and works by using a request-response message exchange pattern:

  1. A client connects to a server and sends a request.

  2. The server processes the request and sends a response back. The connection ends after the client receives the response.

Traditional HTTP connections require vast overhead because the client app is blocked until it receives a response from the server. Also, with this type of connection, the client must always initiate the request. Therefore, the server cannot push notifications to multiple apps at one time.

When a client connects to a server using WebSockets, a persistent connection is established and kept open. Both the client and server can then send multiple messages to each other without needing to follow the HTTP request-response message exchange pattern.

A WebSocket is built on top of the existing HTTP infrastructure, making it easy to reuse infrastructure that is already in place for traditional HTTP.

Common WebSockets Connector Use Cases

WebSockets Connector is useful for real-time apps that must avoid the overhead caused by the HTTP request-response message exchange pattern.

Typical use cases for WebSockets Connector include chat apps and stock quote apps.

Chat Apps

In a chat app, as soon as a client sends a message to a chat group, subscribed participants want to receive that message immediately. If an app uses HTTP, the clients must constantly poll the server for available messages, which slows performance. With WebSockets, the server can push the available message to all subscribed users.

Stock Quotes Apps

Clients can open a WebSocket to a stock quote service, which continuously sends updated prices for a set of stocks. Because such prices change continuously, the ability to reuse the same connection and receive messages at random times make WebSockets an ideal solution for this problem. In this case, the client doesn’t send messages to the server after the connection has been established.

Compatibility

WebSockets Connector is compatible with:

App/Service

Version

Mule

4.2.2 or higher

Java

1.8.0_x

Anypoint Studio

7.4 or higher

Before You Begin

Before creating an app, you must have access to the WebSockets target resource and Anypoint Platform. You must also understand how to create a Mule app using Anypoint Studio.

Basic WebSockets Concepts

The following concepts are integral to understanding how WebSockets Connector works:

Socket Types

There are two types of WebSockets:

  • Inbound
    WebSocket created when the Mule app listens for incoming connections.

    In this scenario, an external system connects to an endpoint exposed by the Mule app.

  • Outbound
    WebSocket created when the Mule app connects to an external system.

    In this scenario, the Mule app initiates communication and asks an external system to establish a WebSocket.

Socket ID

All WebSockets, both inbound and outbound, have a unique socket ID that identifies them. The app must reference these IDs to operate on individual WebSockets.

Groups

WebSockets Connector introduces the concept of groups, which are string identifiers that individual WebSockets can optionally subscribe to. An app can use groups to broadcast one single message to a group of different WebSockets. Groups facilitate the process of identifying which WebSockets should participate in each broadcast.

Subscriptions only exist in the context of each instance of a WebSockets Connector configuration. Therefore, when you establish WebSockets between a Mule app and a remote system, subscribing to WebSockets on Mule’s end of the connection to a group doesn’t affect the WebSockets on the remote system’s end.

Triggers

Triggers initiate the execution of a flow upon receiving a message or event notification. You can specify the following triggers for WebSockets-related events:

  • On New Inbound Message

    Exposes an endpoint that external systems can connect to and initiates the containing flow when a message is received.

  • On New Inbound Connection

    Initiates a flow when a new connection to the Inbound Listener is established.

  • On New Outbound Message

    Exposes an endpoint that initiates a flow when outbound WebSockets receive a message from an external system.

  • On Socket Closed

    Initiates the containing flow when inbound or outbound WebSockets close.

Operations

Anypoint Connector for WebSockets (WebSockets Connector) supports the following operations:

  • Open Outbound Socket

    Connects to an external system and creates an outbound WebSocket.

  • Send

    Sends a message through a WebSocket.

  • Subscribe Groups

    Subscribes a WebSocket to one or more groups. The app can invoke this operation on the same WebSocket multiple times, with each invocation being additive over the previous ones. Repeated groups are ignored.

  • Unsubscribe Groups

    Unsubscribes a WebSocket from one or more groups. You can invoke this operation multiple times on the same socket, with each invocation being additive over the previous ones. Repeated groups are ignored.

  • Broadcast

    Sends a message to multiple WebSockets classified by path, type, and group.

  • Close Socket

    Closes a WebSocket’s connection.

  • Bulk Close Socket

    Closes multiple WebSocket’s connections.

Security

WebSockets Connector supports securing the client and server endpoints through TLS, using the following URI schemes:

  • WS identifies a WebSocket server and resource name without using encryption (for example: ws://localhost:80).

  • WSS identifies a WebSocket server and resource name, secure through TLS (for example: wss://localhost:433).

WebSockets Connector and Runtime Fabric

When you develop a Mule app that uses WebSocket Connector and you deploy Mule into Runtime Fabric, there is no additional configuration required to enable WebSockets Connector in Runtime Fabric.

View on GitHub