# WebSockets

Source: https://crabtalk.ai/docs/guides/sockets

> A client opens a WebSocket on a service's socket tool; the host holds the connection, and the service runs only for the messages on it.

A service takes WebSocket connections on a tool marked `#[tool(socket)]`. How to write one: [WebSockets in code](/docs/development/sockets).

## Connecting

| Where | URL |
|---|---|
| Cloud | `wss://{owner}.crabtalk.app/{service}/sockets/{tool}` |
| A service named after its account, or a custom domain | `wss://{host}/sockets/{tool}` |
| The local host | `ws://127.0.0.1:7777/services/local/{service}/sockets/{tool}` |

A connection signs in as a call to the service's [own MCP server](/docs/guides/service-mcp) does: `Authorization: Bearer` with a CrabTalk API key or token, or a token from the identity provider of the service's app with `--mcp`. A browser's WebSocket sends no `Authorization` header, so a browser connects with no sign-in, to a socket tool declared `access = "anonymous"`. On the local host, the caller is the unverified `x-berm-caller` header.

A connection to a tool that is not a socket tool is refused with [E0114](/docs/errors/E0114).

## Limits

- Messages are text.
- Each node holds a fixed number of open connections across the services it runs. A connection past it is refused with [E0115](/docs/errors/E0115).
- A node restart closes its connections; a client reconnects.

## Cost

Each event is a call of the socket tool, billed at the rates on [Pricing](/pricing):

- Opening a connection counts one run, plus what its `Open` event uses.
- Each message and the close count no run: only the CPU, memory held and rows they use.
- An idle connection costs nothing: between events the service holds no memory.
