honojs / middleware

monorepo for Hono third-party middleware/helpers/wrappers
https://hono.dev
334 stars 109 forks source link

[@hono/node-ws] Duplicate Event Listeners in Middleware #603

Open totoraj930 opened 2 days ago

totoraj930 commented 2 days ago

In upgradeWebSocket, a 'connection' event listener is being set on wss. As the number of new connections increases, the event is called the same number of times.

https://github.com/honojs/middleware/blob/ec6ec4ec02f8db46e3151e7334535e562dfc47e3/packages/node-ws/src/index.ts#L45-L98

In upgradeWebSocket, only event listeners ('message', 'close', 'error') should be set on ws.

Initially, a new wss was created within upgradeWebSocket, so this issue did not occur. However, since wss is now reused, generated in injectWebSocket, this problem has arisen. (https://github.com/honojs/middleware/pull/539)

I attempted to fix the issue, but due to my shallow understanding of the middleware lifecycle, I couldn't succeed. Do you have any ideas?

@nakasyou @mikestopcontinues

inaridiy commented 2 days ago

I'll try to fix it a bit.

inaridiy commented 2 days ago

I fixed some things in the next PR. Maybe it will work.

https://github.com/honojs/middleware/pull/605