Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,113 of 9,113 pages
About the help in other languages
All help pages

Velocity WebSockets, Server-Sent Events and rooms

This page is for developers who want real-time features (chat, live progress, notifications) next to an Exponential site served by Velocity. An ordinary Exponential site does not need them. Applies to Exponential…

Download as PDF

Address: https://doc.exponential.earth/exphelp/viewer/features/6.0/velocity-websockets-and-events.md

On this page

Velocity WebSockets, Server-Sent Events and rooms

This page is for developers who want real-time features (chat, live progress, notifications) next to an Exponential site served by Velocity. An ordinary Exponential site does not need them. Applies to Exponential Velocity 0.0.4.x; overview: Velocity web server.

Velocity has three ways, built into the server, to keep a conversation open between a browser and PHP:

ModelOne PHP process perState livesUse it for
WebSocket connection (socket.io compatible)connectionin that process's statics, until the client leavesper-user state: login, preferences, rate limits, notifications
Roomactive roomin the room's process, shared by every memberchat, game positions, live tallies, shared cursors
Server-Sent Eventsresponsein the script, while it streamsprogress bars, live logs, token streams

The server's own endpoints are /socket.io (the socket.io protocol) and /Q/socket.js (a minimal client). Set Q.socket.io or Q.socket.js to false to switch one off. The dashboard itself uses /Q/ws.

These features belong to the engine's handler model (handlers/<path>.php, dispatched with Q::event()), so an application that wants them uses the engine's application layer.

Stream progress to a browser in one file (Server-Sent Events)

A script that sends Content-Type: text/event-stream is streamed instead of buffered: the headers go out at once with chunked encoding, and every flush() becomes a chunk.

<?php
Q_Response::header('Content-Type: text/event-stream');
Q_Response::header('Cache-Control: no-cache');
for ($i = 1; $i <= 10; $i++) {
    echo 'data: ' . json_encode(['count' => $i]) . "\n\n";
    @ob_flush(); flush();
    sleep(1);
}

Watch it with curl -N https://host/<path of the script>: one data: line arrives per second.

Two other triggers stream a response too: the header X-Accel-Buffering: no, or Q_Response::setStreaming(true). Streaming responses are not stored by the response cache, and they occupy a worker for as long as they run.

Not verified: whether Q.webserver.requestTimeout (default 30 seconds) ends a stream. Test it by streaming for 40 seconds with curl -N and see whether the server cuts the stream; raise the setting if it does.

Per-connection events

Map event names to handlers in the configuration. An event with no mapping uses its own name as the handler path. _connect and _disconnect fire by themselves.

{ "Q": { "webserver": { "sockets": { "events": {
    "_connect":     "auth/login",
    "_disconnect":  "chat/leave",
    "chat/message": "chat/message"
} } } } }
const socket = io('https://host', { transports: ['websocket'] });
socket.emit('chat/message', { text: 'hello' }, (res) => console.log('saved', res.id));

Rooms

{ "Q": { "webserver": { "sockets": { "rooms": {
    "chat/$room": { "handler": "chat/room" },
    "game/$id":   { "handler": "game/room", "tick": 100 }
} } } } }
  • $name in a pattern matches one name (chat/$room matches chat/general).
  • A per-connection handler enters a room with $socket->join('chat/general', [...]). That is the only way in, so access control lives there.
  • The room process dispatches one file per event under the handler prefix: init.php (first member), join.php, leave.php, tick.php (every tick milliseconds), destroy.php (last member left), and one file per message name.
  • When the last member leaves, the process ends.

Settings

Configuration keyDefaultMeaning
Q.webserver.sockets.events.<event>the event nameHandler path for an event; _connect, _disconnect
Q.webserver.sockets.rooms.<pattern>none{handler, tick} of a room
Q.socket.io, Q.socket.js/socket.io, /Q/socket.jsEndpoint paths; false disables
Q.webserver.maxConnections1024Open connections at once (WebSockets count)

Limits

  • A WebSocket connection holds a process; plan memory with the worker pool numbers.
  • Behind a proxy that does not pass upgrades, WebSockets fail. Keep the proxy transparent for Upgrade.
  • The shell's WebSocket accepts the same origin only (Q shell).

Become an Exponential Partner

Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.

Something wrong on this page? Report an error

Documentation license

This documentation is free documentation, licensed under the GNU Free Documentation License 1.3 or later.

Exponential Copyright © 1998-2026 Exponential Foundation and 7x