socketio / socket.io

Realtime application framework (Node.JS server)
https://socket.io
MIT License
61.01k stars 10.1k forks source link

Error occurred when use uWebSocket. #4542

Closed ChoSeoHwan closed 1 year ago

ChoSeoHwan commented 1 year ago

Describe the bug

Error occurred when use uWebSocket.

It runs successfully, but when trying to establish a connection I get the error below.

TypeError: websocket.subscribe is not a function
    at subscribe (C:\Projects\ext-service\.yarn\__virtual__\socket.io-virtual-75b1662c1f\0\cache\socket.io-npm-4.5.4-888fc15f6d-b5456d361b.zip\node_modules\socket.io\dist\uws.js:87:19)
    at Adapter.socket_io_adapter_1.Adapter.addAll (C:\Projects\ext-service\.yarn\__virtual__\socket.io-virtual-75b1662c1f\0\cache\socket.io-npm-4.5.4-888fc15f6d-b5456d361b.zip\node_modules\socket.io\dist\uws.js:22:13)
    at Socket.join (C:\Projects\ext-service\.yarn\__virtual__\socket.io-virtual-75b1662c1f\0\cache\socket.io-npm-4.5.4-888fc15f6d-b5456d361b.zip\node_modules\socket.io\dist\socket.js:298:29)
    at Socket._onconnect (C:\Projects\ext-service\.yarn\__virtual__\socket.io-virtual-75b1662c1f\0\cache\socket.io-npm-4.5.4-888fc15f6d-b5456d361b.zip\node_modules\socket.io\dist\socket.js:338:14)
    at C:\Projects\ext-service\.yarn\__virtual__\socket.io-virtual-75b1662c1f\0\cache\socket.io-npm-4.5.4-888fc15f6d-b5456d361b.zip\node_modules\socket.io\dist\namespace.js:229:24
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

To Reproduce

Server


import { Server } from "socket.io";
import { App } from "uWebSockets.js";

(async () => {
    const io = new Server(80);
    io.attachApp(App());

    io.on('connection', (socket) => {
        console.log('connection');
    });
})();

Socket.IO server version: ^4.5.4 uWebsocket.js version : uNetworking/uWebSockets.js#v20.15.0

Expected behavior

Should work normally with uWebsocket.

Platform:

Additional context Add any other context about the problem here.

frederic117 commented 1 year ago

hello I got the same error thks for your help

darrachequesne commented 1 year ago

I wasn't able to reproduce. Does it work with an older version of uWebsocket?

@frederic117 which OS are you using? Windows 10 too?

frederic117 commented 1 year ago

i just try with the last version uNetworking/uWebSockets.js#v20.15.0 i'm on windows11 thanks for your quick reply :)

ghost commented 1 year ago

Same error here on a MacBook Pro with M1 Pro processor.

socket.io ^4.5.4 uWebsocket.js uNetworking/uWebSockets.js#v20.15.0

gabrielgrijincu commented 1 year ago

Fixed this problem on my side.

Since I already had an app running socket.io with a HTTP server, I was using this app to migrate to µWebSockets. There were two problems:

  1. I was still passing the HTTP server to the socket.io server constructor and the HTTP server was still listening on the main port – that's why the socket had no subscribe method.
  2. I forgot to actually trigger the listen on the µWebSockets app. 🤦🏼‍♂️

Hope it helps. ✨

SOVLOOKUP commented 1 year ago
import { Server } from "socket.io";
import { App } from "uWebSockets.js";

const server = (port = 3000) => {
    const io = new Server()
    io.attachApp(App().listen(port, () => { }))
    return io
}

server()
darrachequesne commented 1 year ago

For future readers: the code above was invalid, it's the uWebSocket server that must listen to incoming connections:

const io = new Server();
const app = App();

io.attachApp(app);
app.listen(80);

Reference: https://socket.io/docs/v4/server-initialization/#with-uwebsocketsjs

Please reopen if needed.

xwiz commented 8 months ago

Fixed this problem on my side.

Since I already had an app running socket.io with a HTTP server, I was using this app to migrate to µWebSockets. There were two problems:

  1. I was still passing the HTTP server to the socket.io server constructor and the HTTP server was still listening on the main port – that's why the socket had no subscribe method.
  2. I forgot to actually trigger the listen on the µWebSockets app. 🤦🏼‍♂️

Hope it helps. ✨

So you separated the http server or you were able to use uws as the http server?