Skip to content

Expose server handlers#166

Merged
ai merged 1 commit intologux:mainfrom
kinday:handler
Mar 7, 2026
Merged

Expose server handlers#166
ai merged 1 commit intologux:mainfrom
kinday:handler

Conversation

@kinday
Copy link
Copy Markdown
Contributor

@kinday kinday commented Mar 5, 2026

This PR extracts WebSocket connection callback into separate handleWebsocketConnection method to BaseServer. This allows to completely bypass internal HTTP server setup and use any external server instead with minor boilerplate.

Example of integration with Fastify:

import fastifyPlugin from 'fastify-plugin'
import * as Logux from '@logux/server'

export default fastifyPlugin<{ path: string }>((fastify, options) => {
  const loguxServer = new Logux.BaseServer({
    disableHttpServer: true,
    minSubprotocol: 1,
    subprotocol: 1
  })

  loguxServer.on('authenticated', (client, latency) => {
    fastify.log.info({ key: client.key, latency, nodeId: client.nodeId, userId: client.userId }, 'client authenticated')
  })

  loguxServer.on('clientError', (error) => {
    fastify.log.error(error, 'client error')
  })

  loguxServer.on('connected', (client) => {
    fastify.log.info({ nodeId: client.nodeId, key: client.key, userId: client.userId }, 'client connected')
  })

  loguxServer.on('disconnected', (client) => {
    fastify.log.info({ nodeId: client.nodeId, key: client.key, userId: client.userId }, 'client disconnected')
  })

  loguxServer.on('error', (error) => {
    fastify.log.error(error, 'failed to process action')
  })

  loguxServer.on('fatal', (error) => {
    fastify.log.fatal(error, 'Logux crashed')
  })

  loguxServer.on('subscribing', (action) => {
    fastify.log.info(action, 'client subscribing')
  })

  loguxServer.on('report', (event, data) => {
    fastify.log.trace(data === undefined ? {} : data, `received "${event}" event`)
  })

  loguxServer.auth(async () => {
    // ...
  })

  loguxServer.channel('user/:id', {
    // ...
  })

  fastify.get(options.path, { websocket: true }, (socket, req) => {
    loguxServer.handleWebsocketConnection(socket, req.raw)
  })
})

P.S.: pnpm added packageManager field automatically and I captured it as a separate commit, can drop if this is not desireable.

@kinday kinday requested a review from ai March 6, 2026 13:10
@ai ai merged commit 5349b27 into logux:main Mar 7, 2026
3 checks passed
@ai
Copy link
Copy Markdown
Member

ai commented Mar 7, 2026

Do you need a release of this API? (for now you can use private addClient())

I am planning to add some extra features before next major release.

@kinday kinday deleted the handler branch March 8, 2026 12:44
@kinday
Copy link
Copy Markdown
Contributor Author

kinday commented Mar 8, 2026

Appreciate the fast turnaround. 💖

The project I’m using that in is in early stages so I don’t mind using the fork for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants