Skip to content

Socket.IO servers

A Socket.IO server in Zorvik is a stand-in for a real one: point your app’s socket.io-client at it and watch every event, answer automatically, or emit events yourself. It speaks Socket.IO 3 and 4 (Engine.IO 4), over HTTP long-polling and WebSocket, including the upgrade from polling to WebSocket that socket.io-client makes by default.

Open the Servers sidebar, click + (New server) and choose Socket.IO server. It listens on 127.0.0.1, port 3003 (or the next port no other saved server uses), and greets each client with a welcome event. Start it, and connect:

import { io } from "socket.io-client";
const socket = io("http://127.0.0.1:3003/chat");
socket.on("welcome", (info) => console.log(info));
socket.emit("hello", "world", (answer) => console.log(answer));

Clients may join any namespace (/, /chat, /admin, …). One client connection shows as one entry in the traffic log, however many namespaces it joins; the log names the namespace before each event (/chat hello).

ModeIn the fileWhat happens
Echo (default)echoEvery event is emitted back to its sender with the same arguments, and acknowledged with them when the client asks (emitWithAck, or a callback as the last argument).
RulesrulesThe first matching rule answers; other events get no answer.
ManualmanualNothing is sent automatically: emit from the traffic panel.
DiscarddiscardEvents are read and dropped.

Each rule reads: when a client emits event with arguments → acknowledge with … then emit … with …, optionally to every client of the namespace, after a delay.

PartIn the fileDescription
EventeventThe event name, or * for any.
Withmatch, patternany arguments (default), arguments contain, are exactly, match regex or arguments (JSON) where (json). The pattern is checked against the arguments as JSON (["hi",{"n":1}]) and, when there is a single text argument, against that text (so are exactly hi matches emit("x", "hi")). A JSON where condition looks into the arguments array: $[0].type == join matches emit("x", {"type": "join"}); see JSON where.
Acknowledge withackArguments (JSON) of the acknowledgement, when the client asks for one. Empty: an acknowledgement without arguments.
Then emitreplyEvent, replyArgsAn event to emit back and its arguments (JSON). Empty event: none.
To every client of the namespacebroadcastEmit the reply to everyone who joined the namespace, the sender included.
AfterdelayMsWait before answering.

Arguments are JSON: an array is several arguments, anything else one. Answers can use the event’s values and templates:

PlaceholderIs
{{event.name}}The event name, as JSON ("hello").
{{event.args}}All arguments as a JSON array.
{{event.arg0}}, {{event.arg1}}, …One argument as JSON (null when missing).
{{$uuid}}, {{$randomFirstName}}, …Dynamic variables.
{{token}}The active environment’s and the workspace’s variables.

The client’s values go in after variables are filled in, so a client sending {{token}} gets that text back, never the variable’s value. Placeholders are JSON values: write [{{event.arg0}}, "ok"], not ["{{event.arg0}}"].

Example rules
- event: join
ack: '{"ok": true, "room": {{event.arg0}}}'
- event: say
match: contains
pattern: hello
replyEvent: said
replyArgs: '[{{event.arg0}}, "{{$isoTimestamp}}"]'
broadcast: true
- event: "*"
replyEvent: unknown
replyArgs: "{{event.name}}"

A rule with an invalid regex is skipped, and the traffic log says so once. A JSON condition that isn’t valid matches nothing.

Event on joining is emitted to each client that joins a namespace, with Arguments (JSON, templates allowed). Leave the event empty for none.

Scheduled events are emitted on a timer to every client of a namespace, whatever the answer mode: a price ticker, a notification every minute, a presence update. Click Add scheduled event and fill in a row:

ColumnIn the fileDescription
CheckboxenabledTurn the event off without removing it.
EventeventThe event name.
ArgumentsargsJSON, an array for several arguments; empty: none. Templates are filled in for each emit, so {"price": {{$randomPrice(5, 50)}}, "at": "{{$isoTimestamp}}"} changes every time.
NamespacenamespaceWho gets it: every client that joined this namespace (default /).
Every (ms)everyMsHow often, at least 10 ms.

The timers run from the moment the server starts; a client that joins gets the next emit, with everyone else. Changing the server’s settings starts the timers again. Arguments that aren’t valid JSON are reported once in the traffic log (Scheduled event 1: The arguments are not valid JSON: …) and that event isn’t emitted until the settings change.

While the server runs, the traffic panel’s composer has an event name, a namespace (/ by default) and the arguments as JSON. Emit sends to the client chosen in Send to, or to every client that joined the namespace.

Disconnect (in the traffic panel) disconnects the client from its namespaces and ends its connection.

  • Path: /socket.io/ unless you change it (socket.io’s path option; clients must use the same).
  • CORS: turn on Allow browsers on other origins for web apps served from another origin (long-polling requests need it; WebSocket doesn’t).
  • TLS: with TLS on, clients connect to https://.
  • Heartbeats: the server pings every 25 seconds and drops clients that don’t answer within 20.
  • Messages and long-polling requests up to 1 MB.
  • Socket.IO 2 clients (Engine.IO 3) are refused with Unsupported protocol version, as a Socket.IO 4 server does.
servers/Chat.yaml
name: Chat
kind: socketio
host: 127.0.0.1
port: 3003
socketio:
mode: rules
greetingEvent: welcome
greetingArgs: '{"id": "{{$uuid}}"}'
rules:
- event: join
ack: '{"ok": true}'
schedule:
- event: price
args: '{"symbol": "ACME", "price": {{$randomPrice(5, 50)}}}'
namespace: /prices
everyMs: 1000
cors: true

To talk to it from Zorvik itself, use a Socket.IO request.