Nuxt Realtime 0.3 is out. 0.2.1 closed the most urgent gaps. This release lets you lock the realtime server down completely: every connection has a verified user, and every read, write and join goes through a hook you control.
The full guide is Securing your app.
Required authentication
One option turns it on:
export default defineNuxtConfig({
nuxtRealtime: {
auth: 'required',
},
})
Connections are authenticated in the new nuxt-realtime:authenticate Nitro hook. It runs before the transport opens, on the polling handshake and on the WebSocket upgrade, so a refused client gets a 401 or 403 and never an open socket.
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('nuxt-realtime:authenticate', async (ctx) => {
const session = await getSession<{ user?: { id: string, name: string } }>(ctx.event, {
password: process.env.SESSION_SECRET!,
})
ctx.user = session.data.user
})
})
With auth: 'required', a connection that no hook identified is refused, the gate hooks deny by default, and presence info and lock ownerInfo stay null until you set them on the server. Leave auth unset to keep accepting anonymous connections.
For a cross-origin realtime server, where cookies don't reach it, send a token through ctx.query in the client nuxt-realtime:auth hook. See token in the query.
Gate hooks for reads and writes
canJoinRoom, canSubscribe and canPublish now have a sibling, canWrite, and canSubscribe also covers state reads (storage:get, storage:subscribe). canForceRelease controls who may take over a lock and always denies by default.
Every gate gets ctx.user and ctx.socket, plus ctx.deny(reason). The reason comes back to the client in the acknowledgement:
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('nuxt-realtime:canWrite', (ctx) => {
const user = ctx.user as { id: string }
if (ctx.key.startsWith(`user:${user.id}:`)) ctx.allow = true
else ctx.deny('Not your key')
})
})
Hooks now vote. Any deny wins, then any allow, then the default, so a later hook can no longer undo an earlier deny. See how hooks combine.
Identity is owned by the server
The verified user lives in socket.data.user and is set before your io.use() middleware runs. No client-supplied field can set the user, the connection owner or lock ownership.
What other clients see in presence and lock info is decided by the new nuxt-realtime:publicInfo hook. The client's value only arrives as ctx.clientInfo:
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('nuxt-realtime:publicInfo', (ctx) => {
const user = ctx.user as { name: string, avatarUrl: string }
ctx.info = { name: user.name, avatarUrl: user.avatarUrl }
})
})
Extending the server
Two new server auto-imports let you build on the same checks the built-in events use:
guard(socket, { kind, name, data })applies the rate limit, size limit and matching gate hook to a custom socket event.useRealtimeServer()gives trusted server codepublish,getStateandsetState, which reach clients on every instance.
nuxt-realtime:afterWrite and nuxt-realtime:afterPublish fire after a successful write or publish, for audit logs, history and webhooks. See extending the server.
Multi-instance
With Redis, presence and lock changes are now relayed to clients on every instance. Each instance also writes a liveness timestamp, and when one is silent for longer than the new instanceTimeoutMs (default 30 seconds) its locks, presence and room membership are released. A crashed instance no longer leaves them behind.
Upgrading
These changes apply even with auth unset:
- A cross-origin WebSocket gets a
403before the upgrade, instead of being opened and closed. - Gate hooks vote:
ctx.allow = trueno longer overrides another hook's deny, andctx.allowstarts asundefined. canJoinRoomruns on every explicit join, so a revoked permission takes effect on reconnect.useRealtimePresence().join()resolves to aboolean, andmembersvalues can benull.- An
authenticatehook that rejects refuses the connection. - Calls dropped by the rate limit or from a rejected socket get a failed acknowledgement instead of hanging.
The full list is in the changelog.