How to initialize a socket.io Server on an already initialized Express App? #4256
-
Hello Sockerio I have a problem trying to instantiate the WS Server on an already initialized Express APP. I followed this guide but still have some issues. The problem is that on client's emit, the emit fails with a network error and on server side looks like the server is not initialized properly. Client error
Server error
For some reason, the server is trying to resolve a file that doesn't exist (!?). Looks like the Server code
Client code
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Anybody that has this problem, the only quick solution is what described here: #4135 . |
Beta Was this translation helpful? Give feedback.
-
Your first example should work properly, as long as you use import * as express from "express";
import * as http from "http";
import { Server } from "socket.io";
const app = express.default();
const httpServer = http.createServer(app); // Where app is the initialized Express App
const wsServer = new Server(httpServer);
wsServer.on('connection', (socket) => {
console.log('a user connected', {socket});
socket.on('ccc', data => {
console.log('message from client', data);
socket.emit('server-message', 'gotcha');
});
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
httpServer.listen(3000); Reference: https://socket.io/docs/v4/server-initialization/#with-express
This error seems totally unrelated to Socket.IO. Could you please check? |
Beta Was this translation helpful? Give feedback.
Your first example should work properly, as long as you use
httpServer.listen()
and notapp.listen()
:Reference: ht…