theturtle32 / WebSocket-Node

A WebSocket Implementation for Node.JS (Draft -08 through the final RFC 6455)
Apache License 2.0
3.75k stars 604 forks source link

websocket doesn't work under bun #454

Open glorand-harmonicinc opened 6 months ago

glorand-harmonicinc commented 6 months ago

Hi, If I run the attached example websocket.zip with nodejs it works:

$ node server.js 
Thu Mar 07 2024 09:39:55 GMT+0000 (Coordinated Universal Time) Server is listening on port 8080
Thu Mar 07 2024 09:40:13 GMT+0000 (Coordinated Universal Time) Connection accepted.
Received Message: 397030
Received Message: 10038402
Received Message: 6845227
Received Message: 2955012
Received Message: 5366268
Received Message: 4668963
$ node client.js 
WebSocket Client Connected
Received: '397030'
Received: '10038402'
Received: '6845227'
Received: '2955012'
Received: '5366268'
Received: '4668963'

, but with bun it doesn't:

$ bun server.js 
Thu Mar 07 2024 09:52:10 GMT+0000 (Coordinated Universal Time) Server is listening on port 8080
Thu Mar 07 2024 09:52:15 GMT+0000 (Coordinated Universal Time) Connection accepted.

$ bun client.js

Same result if I also use nodejs for the client part.

Any idea?

Probably not related to this issue but I also tried nodejs for the server and bun for the client and following error occurs (I have added headers dump in the log trace):

$ node server.js 
Thu Mar 07 2024 10:00:12 GMT+0000 (Coordinated Universal Time) Server is listening on port 8080
Thu Mar 07 2024 10:00:17 GMT+0000 (Coordinated Universal Time) Received request for / with headers {"host":"localhost:8080","sec-websocket-key":"ku8AfoxHZGmLPCmPCxoxlQ==","sec-websocket-protocol":"echo-protocol","sec-websocket-version":"13","upgrade":"websocket","connection":"keep-alive","user-agent":"Bun/1.0.30","accept":"*/*"}
$ bun client.js 
Connect Error: Error: Server responded with a non-101 status: 404 Not Found
Response Headers Follow:
date: Thu, 07 Mar 2024 09:54:38 GMT
connection: keep-alive
keep-alive: timeout=5
transfer-encoding: chunked
theturtle32 commented 6 months ago

Thanks for the report! I haven't tried Bun before. It's likely that there's some API in the http(s) modules that they didn't replicate exactly and its tripping up some expectation in WebSocket-Node. I don't have a lot of time at the moment to dig into it, unfortunately. :-(

rgillan commented 2 months ago

So one of the nice things about Bun is the websocket client/server is natively supported, and using highly performant uWebsockets. The client construct is very similar but the server is very different (although not hard to refactor). Also supports native pub/sub capability.