-
Notifications
You must be signed in to change notification settings - Fork 30
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
WebSocket hooks don't fire when using the exported listen
function from Listhen
#174
Comments
listen
function from Listhenlisten
function from Listhen
Okay I've determined the issue. It turns out the listhen cli does some magic to assign the websocket hooks from the h3 app handler. The raw To get things working I had to change it from listen(toNodeListener(app), {
ws: true,
public: true,
}); to the following listen(toNodeListener(app), {
ws: {
async resolve(info) {
if (app.websocket.resolve) {
return app.websocket.resolve(info);
}
return app.websocket.hooks ?? app.handler.__websocket__ ?? {};
},
},
public: true,
}); Perhaps there should be some documentation added regarding this? Or is the goal that the listhen |
This seems more like a It is also denoted in the README that the CLI does enable it easily |
Hmm, so then I think the main thing that needs to be fixed is updating the exported Right now It looks like the main reason why the type allows for a boolean is because the CLI reuses the ListenOptions type so we would need to some slight modifications to the typing in the CLI to make it work. From an initial glance the cli could prolly use something like this Omit<ListenOptions, "ws"> & { ws?: boolean | CrossWsOptions | ((req: IncomingMessage, head: Buffer) => void) } (This is pseudo code I would need to actually test in an editor haha) I'd be willing to open a PR for this if the team is open to my proposed (pseudo code) solution. Fixing this would remove future confusion surrounding this feature imo, especially since the README does outline the proper usage. Although a more detailed example probably wouldn't hurt either. |
Thanks for investigations and initial reproduction. I think we can do as you proposed in #174 (comment) also in programmatic when |
Okay sure. That would create a more seamless dev experience! I suppose the only blocker for what I wrote in #174 (comment) is that it has access to the original H3 app giving me access to If not we can take a couple approaches:
The second option is prolly more self contained. I can start exploring that unless you think the first option is preferable. |
Exploring with second is good start. We might find a lighter wrapper to replace h3 layer in both places |
Environment
NodeJS - v20.11.1
H3 - v1.11.1
Reproduction
https://github.com/joshmossas/h3-ws-listhen-bug
Describe the bug
If you start an h3 server like so:
None of the websocket hooks in
defineWebSocketHandler
will fire. Oddly enough, clients are still able to connect and send messages. So listhen is upgrading the connection, but for some reason none of the hooks go off meaning you can't actually send messages back.If you refactor the code to use the listhen cli. It starts working again:
Additional context
I'm unsure if the bug is happening on the H3 side or the Listhen side. I did try digging through both code bases for a while and couldn't figure out what was causing this behavior.
Logs
No response
The text was updated successfully, but these errors were encountered: