> ## Documentation Index
> Fetch the complete documentation index at: https://docs.genesiscomputing.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Teams Socket Delivery (Preview)

> Receive Microsoft Teams activities over outbound WebSockets instead of a public HTTPS messaging endpoint: when to choose it, how to turn it on in Teams Developer Portal or the Teams CLI, and how Genesis keeps one set of sockets per deployment.

The Teams SDK connector (`teams_v2`) normally receives activities on its HTTPS messaging endpoint, `POST /api/teams/v2/messages/{bot_id}`, which Teams must be able to reach from the internet. **Socket delivery** turns that around: Genesis opens outbound WebSockets to Teams, and activities arrive over them. No inbound endpoint, tunnel or certificate is needed.

> [!WARNING]
>
> Socket mode is a Microsoft **public preview**, available only for **newly created bots**, and may change before general availability. Microsoft recommends an HTTPS messaging endpoint for agents in production, and **requires** one for apps published to the Teams Store. Keep `teams_v2.delivery` on `https` unless the HTTPS endpoint cannot be reached.

## When to choose socket delivery

Choose it when Genesis cannot accept inbound HTTPS from Microsoft:

* Genesis runs behind a firewall or on a private network with no public ingress.
* You are developing or evaluating against a new bot and do not want to publish an endpoint or run a tunnel.

Keep **HTTPS** (the default) for production deployments, for any app that will be published to the Teams Store, and for existing bots (socket mode is not offered for them). Teams bots are not offered in a Databricks App, so no socket is opened there either.

The network needs outbound HTTPS on port 443 to `amer.botapi.skype.com`, `emea.botapi.skype.com` and `apac.botapi.skype.com`, and any proxy or firewall must allow WebSocket upgrades and long-lived connections.

## Turn it on

The bot registration and Genesis must agree. Switch both.

### 1. Switch the bot registration

**In Teams Developer Portal:**

1. Go to **Tools** > **Bot management** and select the bot.
2. Select **Configure**.
3. Under **Delivery method**, select **Socket mode**, and then select **Save**.

**Or with the Teams CLI** (version 3.1.0 or later):

```bash theme={null}
npm install -g "@microsoft/teams.cli@>=3.1.0"
teams login
teams app update <app-id> --messaging-mode socket
```

Teams picks up the change within about a minute. From then on it delivers activities only over connected sockets, never to the messaging endpoint, so nothing reaches Genesis until step 2 is done.

### 2. Switch Genesis

Set `teams_v2.delivery` to `socket` (Settings, or the `TEAMS_V2_DELIVERY` environment variable). `teams_v2.enabled`, `teams_v2.app_id` and `teams_v2.client_secret` must already be set, as for HTTPS delivery.

The sockets carry the activities of one agent: the one named in `teams_v2.socket_agent_id`, or, when that is empty, the one agent with a Teams adapter. With several such agents, set `teams_v2.socket_agent_id`; until then no socket is opened and the status says `no_agent`.

Genesis reads these settings every 15 seconds, so no restart is needed.

## What Genesis does

* **One server holds the sockets.** Teams allows only a few sockets per app and region (more are refused with HTTP 429). On a deployment with several Genesis servers, the one holding the `teams_v2_socket` lease in the metadata database opens them. It renews the lease every 15 seconds. When that server stops, another takes the lease and reconnects within a minute; a clean shutdown hands over at once.
* **One socket per region** (amer, emea, apac), as Teams requires. A dropped region reconnects by itself with backoff, honoring Teams' `Retry-After`. If a whole start fails, or Teams refuses a region for good (for example after the app secret was rotated), Genesis closes the sockets and starts again after 5 seconds, then 15, 30, 60, 120 and every 300 seconds, reading the app's credentials again each time.
* **Fails closed.** A server that cannot read the lease or its settings closes its sockets, so two servers never hold them at once. A shutting-down server closes its sockets and frees the lease first, then gives answers already running up to 10 seconds to finish.
* **Same handling as HTTPS.** Messages, group chats, channels, Approve and Deny cards, question cards and account links behave exactly as they do over HTTPS. The serviceUrl and tenant allowlists apply too.
* **The 25-second reply limit.** Teams fails an activity whose reply comes later than 25 seconds. Genesis acknowledges a message at once and sends the agent's answer separately, as it always does. A card click whose decision takes longer than 20 seconds is answered "Still working on this"; when the request is approved or denied, the card is redrawn for everyone. A click that ends in a refusal after that point is logged and counted, but its clicker sees no further message.

## Check its status

Admins see a **Microsoft Teams delivery** card on the **Workspace Policies** page, read from `GET /api/teams/v2/socket/status`:

| Field | Meaning |
| - | - |
| `state` | `off`, `standby` (another server holds the sockets), `connecting`, `ready`, `degraded` (a region is reconnecting), `backoff` (the last start failed, or Teams refused a region for good; reopened shortly), `no_agent`, `no_app` (no `teams_v2.app_id`), `no_secret` (the app id is set but its client secret is missing or cannot be read), `lease_unreadable` or `settings_unreadable` (this server holds no socket until it can read them) |
| `holds_lease`, `holder`, `lease_holder` | Whether this server holds the sockets, its own name, and who holds them now |
| `geos` | Each region's socket status |
| `reconnects`, `failed_starts`, `late_answers`, `late_refusals` | Region reconnects, failed starts in a row, activities answered before their work finished, and those of them that then ended in a refusal Teams was not told about |
| `last_error`, `last_error_at` | The last socket error, for example Teams refusing the app |

Server logs carry the same events under `Teams v2 socket:`.

## Troubleshooting

| Symptom | Cause and fix |
| - | - |
| `state` is `backoff` and `last_error` mentions 403 `Socket mode is not enabled for this bot` | The bot registration is still on HTTPS, or socket mode is not available for the tenant yet. Do step 1 and wait about a minute. |
| `state` is `no_secret` | `teams_v2.app_id` is set but `teams_v2.client_secret` is empty or cannot be read. Set the secret; Genesis tries again at its next check, within 15 seconds. |
| A region shows `stopped`, then `state` is `backoff` | Teams refused the app for good (401 or 403): check `teams_v2.app_id` and the client secret. Genesis retries with the stored credentials after the backoff. |
| 429 in the logs | More sockets are open for this app than Teams allows: another deployment, or a second environment, uses the same bot. Give each its own bot. |
| `standby` on every server | Another server, possibly a stopped one whose lease has not run out yet, holds the lease. It is free again within 45 seconds. |
| Activities still go to the HTTPS endpoint | The registration change has not taken effect yet. Wait about a minute. |

## Switch back to HTTPS

Set `teams_v2.delivery` to `https`, then switch the registration back: in Developer Portal select **HTTPS endpoint** and enter the messaging endpoint URL, or run `teams app update <app-id> --messaging-mode http --endpoint <https-url>`. Genesis closes its sockets and frees the lease at its next tick.

Microsoft's reference: [Configure the delivery method for an agent](https://learn.microsoft.com/en-us/microsoftteams/platform/agents-in-teams/configure-delivery-methods-for-agents).
