Reputation: 113
In my project, i have some sockets which needs to be authenticated and some sockets which are not containing the auth-token.
const socket = require('socket.io');
let server = require('http').createServer(app);
require('./config/authSockets')(socket(server));
require('./config/sockets')(socket(server));
I have created separate files for this scenario that in authSockets, first it should check that if socket has the token then it'll open the connection only and emit those sockets. and the next file which is ./config/sockets this file should run everytime and it donot require the socket. but my code isn't working, its always only accept the second file.
Upvotes: 2
Views: 302
Reputation: 707148
I would suggest you use socket.io namespaces and you require authentication for one of the namespaces and not the other. Both namespaces are served by the same socket.io server, but you can handle each namespace differently (requiring auth for one, but not the other). This is a perfect application for socket.io namespaces (two separate purposes sharing the same socket.io server and client connection).
Working socket.io across two files is done by importing and exporting the right things. I've answered questions like that many times before here, but to help you specifically, I'd have to see your actual code (pasted into your question) to know exactly what module sharing solution to suggest.
Upvotes: 0