WebSockets
Adapters
Plain WebSocket and Socket.IO v4 on the same gateways.
The same gateways speak plain WebSocket and Socket.IO v4; you write nothing extra.
Socket.IO
The same gateways answer Socket.IO v4 clients, with nothing extra to write. useWebSockets() also mounts /socket.io, and a client names the gateway as its namespace:
import { io } from "socket.io-client";
const chat = io("http://localhost:8080/chat", { transports: ["websocket"], query: { name: "ann" }, auth: { token } });
chat.emit("echo", "hi", (reply) => console.log(reply)); // "HI": the handler's return value is the acknowledgement
chat.emit("join", "room1");
chat.on("message", (m) => console.log(m.from, m.text)); // server.to("room1").emit("message", ...)| Socket.IO | Kest |
|---|---|
namespace /chat | @WebSocketGateway("/chat") |
socket.emit("say", payload, ack) | @SubscribeMessage("say"); the first argument is the payload, the return value is the acknowledgement |
socket.on("message") | client.send("message", data), server.to(room).emit(...), server.broadcast(...) |
auth object | client.attributes["auth"] (set before handleConnection) |
query, headers | client.request |
| a refused connection | client.close() in handleConnection disconnects the client |
| ping and pong | the server pings every 25 s and closes after 20 s without a pong (SocketIoOptions) |
useWebSockets(socketIo = null) turns it off, or pass SocketIoOptions().apply { path = "/ws"; pingIntervalMs = 10_000 }.
WebSocket transport only
The client must use transports: ["websocket"]. The default Socket.IO client starts with HTTP long polling and then upgrades, which is not implemented; with the default it cannot connect. Binary attachments, and acknowledgements the server asks of a client, are not implemented either.
What is tested
A client written from the protocol specification checks the Engine.IO open packet, namespace connect, an event with an acknowledgement id, rooms and the sender being excluded, an unknown namespace, an unknown event, a gateway that refuses, and ping and pong. The official socket.io-client is exercised by an optional test that runs only when you npm install socket.io-client@4 in examples/platform-tests/build/sio-client (the build never downloads it), so it was not run in the last full build.
Not built
Plain WebSocket and Socket.IO work on the Netty engine only. Rooms are per instance (see above), so a Socket.IO adapter for Redis does not exist.
Several instances: the Redis adapter
With more than one instance behind a load balancer, server.broadcast(...) and server.to("room").emit(...) only reach the clients connected to that instance. Attach an adapter and they reach every instance:
// build.gradle.kts: implementation("io.kest.framework:websocket-redis")
val app = Kest.run<AppModule>()
app.http(adapter) { useWebSockets() }
app.get<WsServer>().useAdapter(RedisWsAdapter("redis.internal", 6379))A client joins a room on the instance it is connected to; a message to that room, sent from any instance, reaches the members on all of them. The sender (except = client) is excluded, and an instance ignores its own message when it comes back, so nobody gets a message twice.
- Carried across instances:
broadcastandto(room).emit. Stays on one instance:client.send(a client has one connection),server.clients(),room.size. - At most once. Redis pub/sub does not store messages: an instance that loses its Redis connection misses what was sent meanwhile, then reconnects by itself. For messages that must not be lost, use a queue.
- Write your own adapter by implementing
WsAdapter(publish,subscribe,close) over any message bus. - Verified with two instances and a real Redis, over plain WebSocket. Socket.IO clients use the same server methods, but that combination has not been run separately.