---
schema_version: "1"
id: 7d55de5c477d394c4713cb60e7c5e3bb61ecd8d697c540c47cb9092b2281b7e3
title: WebSockets Managing Groups
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/websockets-connector/latest/websockets-connector-managing-groups.md
canonical_url: https://docs.mulesoft.com/websockets-connector/latest/websockets-connector-managing-groups
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 033d1c6ad1d8583cd2c0cd1351f70a2c9a6b7096c09c7a25b86deef2bb745fcf
description: MuleSoft Documentation Site
release: "1.0"
latest: true
---
---
product: WebSockets Connector
version: 1.0
is-latest-version: true
---

# WebSockets Managing Groups

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

The WebSockets Connector uses socket groups, which are String identifiers that WebSockets clients
can optionally subscribe to. This is useful for what we call message segregation, which enables your
app to broadcast a single message to a group of different WebSockets clients.

Examples of how to leverage groups can be found in the documentation of the broadcast operation.

Both the `<websocket:inbound-listener>` trigger and the `<websocket:open-outbound-socket>` operation
allow to specify default groups. That is groups that every new inbound and outbound sockets will be
automatically subscribed to.

There are also use cases that require manually subscribing or unsubscribing a socket from a group.
For example, imagine the use case of a multi-tenant application in which an inbound WebSocket is
established. The initiator HTTP request includes an authentication header called `clientId`. We call
a flow called `fetch-client-details` which validates a client ID and sets a variable called `orgId`
that identifies the organization the client belongs to. The socket is added to a group that
identifies its organization.

Group subscription is dynamic because the group to be subscribed to (the organization ID) can only
be determined at runtime.

An example of a flow:

```xml
<flow name="onIncomingMessageFlow">
    <websocket:inbound-listener path="/feed" config-ref="ws" />

    <!-- Store relevant information in variables -->
    <set-variable variableName="socketId" value="#[attributes.socketId]" />
    <set-variable variableName="authHeader" value="#[attributes.headers.clientId]" />

    <!-- Validate the auth Header -->
    <flow-ref name="fetch-client-details" />

    <!-- Perform group subscription -->
    <websocket:subscribe-groups
      socketId="#[vars.socketId]"
      groups="#[[vars.orgId]]"
      config-ref="ws" />
</flow>
```

The flip side of this use case is that you also need to dynamically unsubscribe from a group, which
can be achieved with the `<websocket:unsubscribe-groups>` operation:

```xml
<websocket:unsubscribe-groups
  socketId="#[vars.socketId]"
  groups="#[[vars.orgId]]"
  config-ref="ws" />
```

## See Also

- [Salesforce Help](https://help.salesforce.com)

- [WebSockets Connector Overview](https://docs.mulesoft.com/websockets-connector/latest/)

- [Connector Configuration](https://docs.mulesoft.com/websockets-connector/latest/websockets-connector-config-topics)

- [Messaging Operations](https://docs.mulesoft.com/websockets-connector/latest/websockets-connector-messaging-operations)
