-
-
Notifications
You must be signed in to change notification settings - Fork 665
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
How to integrate Socket.io? #107
Comments
Having the same issue, search many places and seem like socket.io will need a server instance to create the io object. Ref: https://github.com/luixaviles/socket-io-typescript-chat/blob/master/server/src/server.ts |
This seems like a very clean approach. import * as sio from 'socket.io';
const chatServer = sio({
path: '/socket.io/chat'
});
chatServer.on('connection', (socket) => {
console.log('a user connected');
socket.on('chat message', (msg) => {
console.log('message: ' + msg);
chatServer.emit('chat message', msg);
});
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
export default chatServer;
/// ________________________ later in main server file ____________________________________-
// socket.io
const sioModules = require('require-all')({
dirname: __dirname + '/socket.io',
filter: (filename: string) => {
filename = filename.toLowerCase();
if ((filename.endsWith('.ts') && !filename.endsWith('.spec.ts'))
|| (filename.endsWith('.js') && !filename.endsWith('.spec.js'))) {
return filename.substr(0, filename.length - 3);
}
}
});
for (const name of Object.keys(sioModules)) {
const exported = sioModules[name].default;
if (exported && exported.constructor.name === 'Server') {
console.log(`Add socket.io server ${name}`);
const sioServer = exported as SocketIO.Server;
sioServer.attach(server);
}
} |
I could include socket.io doing this :
|
please help me integrating socket.io with this repo
The text was updated successfully, but these errors were encountered: