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:
| Model | One PHP process per | State lives | Use it for |
|---|---|---|---|
| WebSocket connection (socket.io compatible) | connection | in that process's statics, until the client leaves | per-user state: login, preferences, rate limits, notifications |
| Room | active room | in the room's process, shared by every member | chat, game positions, live tallies, shared cursors |
| Server-Sent Events | response | in the script, while it streams | progress 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.
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 }
} } } } }$namein a pattern matches one name (chat/$roommatcheschat/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(everytickmilliseconds),destroy.php(last member left), and one file per message name. - When the last member leaves, the process ends.
Settings
| Configuration key | Default | Meaning |
|---|---|---|
Q.webserver.sockets.events.<event> | the event name | Handler 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.js | Endpoint paths; false disables |
Q.webserver.maxConnections | 1024 | Open 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).
Related pages
- Velocity web server, Velocity scheduler, control panel and dashboard, discovery, federation and deploy
- Velocity worker pool and compatibility layer
- Changelog: Exponential Velocity engine
- History: July 2026 (long-lived processes, socket.io), August 2026 (socket.io in all browsers, Server-Sent Events)
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.