fastify/fastify-websocket

Cannot access websocketServer instance

bahamut657 opened this issue · 1 comments

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.24.1

Plugin version

4.0.0

Node.js version

14.15.4

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.0.1

Description

It seem impossible to access the decarated properties websocketServer (the istance of the WS lib).
I need it to access currently connected clients.

Steps to Reproduce

const fastify = require("fastify")();
fastify.register(require('fastify-websocket'));
console.log(server.websocketServer); //=> prints undefined

Expected Behavior

server.websocketServer is the real WebSocketServer instance (from ws lib)

Plugin registration in fastify is async, so the initialization logic for fastify-websocket has not run by the time you access the property because you're doing it synchronously. If you await the registration, or access the property after await server.ready(), it should be there! If not, we can reopen and debug.