nitro icon indicating copy to clipboard operation
nitro copied to clipboard

feat(dev, node-server): support a hook to access Node.js server instance

Open OneCalmCloud opened this issue 2 years ago โ€ข 4 comments

๐Ÿ”— Linked issue

solve https://github.com/unjs/nitro/issues/2040

โ“ Type of change

  • [ ] ๐Ÿ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • [ ] ๐Ÿž Bug fix (a non-breaking change that fixes an issue)
  • [ ] ๐Ÿ‘Œ Enhancement (improving an existing functionality like performance)
  • [x] โœจ New feature (a non-breaking change that adds functionality)
  • [ ] ๐Ÿงน Chore (updates to the build process or auxiliary tools and libraries)
  • [ ] โš ๏ธ Breaking change (fix or feature that would cause existing functionality to change)

๐Ÿ“š Description

Hi ๐Ÿ‘‹ this PR can be possible to enable WebSocket and Web Server to share the same port without the need for reverse proxy.

๐Ÿ“ Checklist

  • [x] I have linked an issue or discussion.
  • [ ] I have updated the documentation accordingly.

OneCalmCloud avatar Dec 30 '23 07:12 OneCalmCloud

When will this PR be added to the main branch?

JCtapuk avatar Feb 12 '24 04:02 JCtapuk

I propose to expand these events

const server =
  cert && key
    ? new HttpsServer({ key, cert }, toNodeListener(nitroApp.h3App))
    : new HttpServer(toNodeListener(nitroApp.h3App));
    
// others code
nitroApp.hooks.callHook("node.server.init", server);

server.on('upgrade', (req: IncomingMessage) => {
   nitroApp.hooks.callHook("node.server.upgrade", server, req);
})

server.on('listening', () => {
   nitroApp.hooks.callHook("node.server.listening", server);
})

So much more stable for more control. For example, a handshake for WebSocket and the event that the server has started. As well as initialization which will make it possible to register more events for advanced users

JCtapuk avatar Feb 12 '24 05:02 JCtapuk

When will this PR be added to the main branch?

I'm so sorry for the delay in progressing the PR due to my unfamiliarity with GitHub functionalities; I'm actively learning and seeking guidance to contribute effectively to the project.

OneCalmCloud avatar Feb 12 '24 15:02 OneCalmCloud

Sorry for delay on this. I guess now that we have official WebSocket support, it should be less priority but I will defneitly investigate how to make it an extendable addon before introducing ๐Ÿ‘๐Ÿผ

pi0 avatar Feb 27 '24 17:02 pi0