HUB
Sign inSign up

cattleman/sockets

By cattleman

•Updated about 2 hours ago

Cattleman Sockets for local development - Pusher-compatible WebSockets, as on Cloudflare

Image
0

416

cattleman/sockets repository overview

⁠Sockets

Cattleman⁠ Sockets for local development: Pusher-compatible WebSockets, run on your machine as they run on Cloudflare.

Sockets is a realtime server that speaks the Pusher protocol, deployed through Cattleman as a Cloudflare Worker. This image is for building an app against it without a deployment: pusher-js, Laravel Echo and the Pusher server SDKs connect to it as they would to your worker.

It isn't an imitation. The image runs the worker bundle Cattleman deploys, in workerd⁠, the runtime Cloudflare runs it in. The protocol, error codes, limits and webhook signatures are the ones you get in production.

⁠Quick start

docker run --rm -p 6001:6001 cattleman/sockets

It starts with one app and prints the lines for your app's .env:

CATTLEMAN_APP_ID=app-id
CATTLEMAN_APP_KEY=app-key
CATTLEMAN_APP_SECRET=app-secret
CATTLEMAN_HOST=localhost
CATTLEMAN_PORT=6001
CATTLEMAN_SCHEME=http

A browser client connects with pusher-js:

const pusher = new Pusher("app-key", {
  wsHost: "localhost",
  wsPort: 6001,
  forceTLS: false,
  enabledTransports: ["ws"],
  cluster: "",
});

GET /ready answers {"ok":true} for health checks, and the image has a HEALTHCHECK that uses it.

⁠Docker Compose

services:
  sockets:
    image: cattleman/sockets:1
    ports:
      - "6001:6001"
    environment:
      SOCKETS_APP_ID: my-app
      SOCKETS_APP_KEY: my-key
      SOCKETS_APP_SECRET: my-secret

  app:
    # ...
    depends_on:
      sockets:
        condition: service_healthy

From another Compose service the host is the service name (sockets); from a browser on your machine it is localhost.

⁠Settings

Settings are environment variables. The limits are the ones on a deployment's settings page in Cattleman, with the same defaults and ranges.

VariableDefault
SOCKETS_APP_IDapp-idThe app's id
SOCKETS_APP_KEYapp-keyThe app's key
SOCKETS_APP_SECRETapp-secretThe app's secret
SOCKETS_WEBHOOKS[]The app's webhooks, as a JSON list
SOCKETS_APPS_FILEA JSON file of apps, in place of the four above
SOCKETS_PORT6001Port to listen on
SOCKETS_DEBUGfalseLog connections, subscriptions, events and webhooks (below⁠)
SOCKETS_MAX_CONNECTIONS1000000Concurrent connections (1-10000000)
SOCKETS_ENABLE_CLIENT_MESSAGEStrueAllow client-* events on private and presence channels
SOCKETS_CLUSTER_MODEfalseSpread connections across shards
SOCKETS_CLUSTER_CONNECTIONS_PER_SHARD600Connections per shard in cluster mode (100-10000)
SOCKETS_MAX_BACKEND_EVENTS_PER_SECOND10Events through the REST API per second (1-1000)
SOCKETS_MAX_CLIENT_EVENTS_PER_SECOND100Client events per second, per connection (1-1000)
SOCKETS_MAX_READ_REQUESTS_PER_SECOND10REST API reads per second (1-1000)
SOCKETS_MAX_PRESENCE_MEMBERS_PER_CHANNEL100Members in one presence channel (1-1000)
SOCKETS_MAX_PRESENCE_MEMBER_SIZE_IN_KB10Size of one presence member's data (1-100)
SOCKETS_MAX_CHANNEL_NAME_LENGTH200Channel name length (50-500)
SOCKETS_MAX_EVENT_CHANNELS_AT_ONCE10Channels one event may target (1-100)
SOCKETS_MAX_EVENT_NAME_LENGTH200Event name length (50-500)
SOCKETS_MAX_EVENT_PAYLOAD_IN_KB300Event payload size (10-1000)
SOCKETS_MAX_EVENT_BATCH_SIZE10Events in one batch_events request (1-100)

A value that isn't valid stops the server before it starts, and says why. docker run --rm cattleman/sockets --help lists the settings of the version you have.

⁠Several apps

Mount a JSON file and point SOCKETS_APPS_FILE at it. Each app can set its own limits and webhooks; the two cluster settings apply to the whole server.

[
  { "id": "shop", "key": "shop-key", "secret": "shop-secret" },
  { "id": "chat", "key": "chat-key", "secret": "chat-secret", "enableClientMessages": false }
]
docker run --rm -p 6001:6001 \
  -v ./apps.json:/etc/sockets/apps.json:ro \
  -e SOCKETS_APPS_FILE=/etc/sockets/apps.json \
  cattleman/sockets
⁠Webhooks
docker run --rm -p 6001:6001 \
  -e SOCKETS_WEBHOOKS='[{"url":"http://host.docker.internal:8000/pusher/webhooks","event_types":["channel_occupied","channel_vacated"]}]' \
  cattleman/sockets

event_types takes channel_occupied, channel_vacated, member_added, member_removed, client_event and cache_miss. An optional "filter": { "channel_name_starts_with": "presence-" } limits a webhook to matching channels. Requests are signed as in production, with X-Pusher-Key and X-Pusher-Signature.

Webhooks are sent from inside the container. Address an app in the same Compose project by its service name, and one running on your machine by host.docker.internal (on Linux, add --add-host=host.docker.internal:host-gateway).

⁠Seeing what happens

Set SOCKETS_DEBUG=1 and the server logs a line for each connection, subscription, published event and webhook, and says why anything was refused:

docker run --rm -p 6001:6001 -e SOCKETS_DEBUG=1 cattleman/sockets
14:02:11.531 [app-id] 5197641.2897028 connected
14:02:11.642 [app-id] 5197641.2897028 subscribed to private-orders
14:02:12.001 [app-id] POST /apps/app-id/events: 200
14:02:12.003 [app-id] published order.shipped on private-orders to 1 connection
14:02:12.330 [app-id] 5197641.2897028 sent client-typing on private-orders
14:02:12.334 [app-id] webhook client_event to http://app/pusher/webhooks: 200
14:02:15.120 [app-id] 8802193.5512907 was refused private-orders: The connection is unauthorized. (401)
14:02:16.004 [app-id] POST /apps/app-id/events: 401 The request signature is invalid.
14:02:20.918 [app-id] 5197641.2897028 disconnected

Each line starts with the time (UTC) and the app, and a connection is named by its socket id. published ... to nobody means the event was accepted but no connection was subscribed to that channel. Event payloads aren't logged, and nor are health checks.

⁠What differs from a deployment

  • It listens on plain ws:// and http://, where a deployment is wss:// and https:// on port 443.
  • Settings are read at start. Restart the container to change one; a deployment picks a change up within a minute.
  • In cluster mode, shards aren't pinned to the EU jurisdiction, which only exists on Cloudflare.
  • Everything is held in memory by one process. A restart drops connections and cached-channel payloads, and clients reconnect on their own.
  • The debug log is only here: a deployment doesn't have it.

⁠Tags

The image's version is the worker's: cattleman/sockets:1.2.1 runs the 1.2.1 worker, so you can develop against the version your deployment is on. 1.2 and 1 follow the newest patch and minor release, and latest the newest release. Images are built for linux/amd64 and linux/arm64.

⁠Licence and source

Sockets is a derivative of soketi⁠ and is licensed under the GNU Affero General Public License v3⁠. The image contains the source it was built from, in /app, with the licence text in /app/LICENSE:

docker run --rm --entrypoint tar cattleman/sockets -c -C /app --exclude=node_modules . > sockets-source.tar

Tag summary

Content type

Image

Digest

sha256:4ddcc810a…

Size

123.4 MB

Last updated

about 2 hours ago

docker pull cattleman/sockets