Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not access globalThis.__io #12

Open
duytnb79 opened this issue Dec 27, 2023 · 4 comments
Open

Can not access globalThis.__io #12

duytnb79 opened this issue Dec 27, 2023 · 4 comments

Comments

@duytnb79
Copy link

When I access globalThis.__io, it always return undefined.
How can I fix it ?
Thank you for supporting.

Additional, I use environment io.to(roomId).emit('function') to send message to specific room in socket.
I think we need to guide to use it.

@duytnb79
Copy link
Author

duytnb79 commented Dec 27, 2023

Sorry for not clearly, I mean I want to use globalThis.__io in development environment. But I see this code in file server.ts

export function createIOHandler<T extends Record<string, (io: SocketServer) => void>> (functions: T, serverOptions: Partial<ServerOptions>) {
  return eventHandler((event) => {
    if (!globalThis.__io && process.env.NODE_ENV === 'production') {
      const httpServer = (event.node.req.socket as any).server as Server
      const io = new SocketServer(httpServer, serverOptions)

      Object.keys(functions).forEach((fn) => {
        functions[fn](io)
      })

      globalThis.__io = io
    }
  })
}

Can you point me how to use it.

@felixrydberg
Copy link

I found this aswell. I have no clue why its only set to run this code while in production and not in development

@ivan-skjavik
Copy link

Is it possible to access the socket from middlewares so that it can be attached to the context?

@felixrydberg
Copy link

You would have to set your .env.NODE_ENV = 'production'

However. Nitro seems to have issues with websockets, it doesnt allow new connections after 6 clients has connected even if some of them disconnect. I would recommend following this thread as it seems like Nitro is working on websockets support.
nitrojs/nitro#678

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

No branches or pull requests

3 participants