h3js / h3

⚡️ Minimal H(TTP) framework built for high performance and portability
https://h3.dev
MIT License
4.12k stars 259 forks source link

Websocket & middlewares #999

Open horvbalint opened 1 month ago

horvbalint commented 1 month ago

Describe the feature

Last week I wanted to add websocket to an existing nitro app, but ran into some unexpected behaviours. I think fixing/improving these would be beneficial for everyone.

I have two main parts of my api /public/* and /protected/*. There is a middleware registered, that is authenticating the user on every /protected/* route and also adds the user object to event.context. My plan was simple, create a /protected/ws.ts route, that is using defineWebsocketHandler. This way (in my plan) I wouldn't need to duplicate the authentication logic and everything should work nicely. This is sadly not the case.

Additional information

horvbalint commented 1 month ago

I think this would also fix https://github.com/unjs/h3/issues/715