WebSocket logging
Log WebSocket open, message, and close events
Use wrapWs on the plugin instance to add lifecycle logs for WebSocket routes:
import { Elysia } from 'elysia'
import logixlysia from 'logixlysia'
const plugin = logixlysia({
config: {
service: 'chat'
}
})
interface ChatWs {
send(data: unknown): void
}
const app = new Elysia().use(plugin).ws('/chat', {
...plugin.wrapWs('/chat', {
open(ws) {
;(ws as unknown as ChatWs).send('connected')
},
message(ws, message) {
;(ws as unknown as ChatWs).send(message)
},
close(ws) {
console.log('bye', ws.id)
}
})
})
The hooks type the socket as the plugin’s WebSocketLike, which does not declare send, so the example casts it to a local ChatWs; a typed default is a planned library improvement.
On Elysia 2, a .ws() route also needs Elysia’s WebSocket capability, .use(websocket()) from elysia/websocket (see Elysia 2 beta). On Elysia 2.0.0-rc.5 and later, a WebSocket route needs Bun 1.4.4 or newer. On an older Bun the upgrade request answers 500 and the socket never opens, with or without Logixlysia.
A hook may reply by returning a value, a promise or a generator. A value reaches Elysia as it is. A promise is passed on as a promise, and a generator it resolves to is wrapped the same way as one returned directly. Only a plain generator is wrapped, and only from an open or message hook: what a function* or async function* in this realm returns, with no properties added to it or to its function’s prototype. The wrapper is a generator of the same kind and forwards every value, so its line can wait until iteration ends. Anything else is returned unchanged and logged when the hook returns: a hand-written iterator, a generator from another realm such as a vm context, and a generator that carries properties of its own.
Each lifecycle event logs with HTTP method WS, the route path, the connection’s age (shown where an HTTP line shows its duration), and optional wsId in context, and a hook that succeeded logs status 200. Your close hook receives the WebSocket’s close code and reason, and the “WebSocket closed” line logs them too.
A hook that throws or rejects gets an ERROR line in place of its usual line. Its message names the hook and the error, for example WebSocket message hook failed: boom, so a log file shows the cause as well. The line carries the error in its error field, as an HTTP error line does, and no status, since the client was sent none; the error still reaches Elysia’s onError. An open or message hook that returns a plain generator is logged when its iteration ends, so the connection’s age on that line includes the time spent sending, and a throw inside the generator gets the same ERROR line. A generator Elysia never finishes gets no line. Any other result is logged when the hook returns or its promise settles. A close hook does not wait on a generator it returns: it is logged when it returns or its promise settles, whatever it returned, and the connection’s context is cleared then.
Request context on WebSockets
Accumulate context with the WebSocket instance as the key:
.ws('/chat', {
...plugin.wrapWs('/chat', {
message(ws, payload) {
plugin.store.logger.mergeContext(ws, { lastPayloadType: typeof payload })
}
})
})
The plugin stores context per connection, so a value merged in open is still there on later message and close lines.
Disable WebSocket logs
logixlysia({
config: {
disableWebSocketLogging: true
}
})