Reputation:
I'm handling over 15 different socket events, I'd like to manage certain socket.io events within the modules that are related to those events.
For example, I'd like to have a file named login.js handle the login
socket event, and a file named register.js handle the registration socket event.
index.js:
socket.on("connection", function (client) {
console.log("Client connected to socket!");
client.on("login", function (data) {
validate(data){
socket.sockets.emit("login_success", data);
}
});
client.on("register", function (data) {
register(data){
socket.sockets.emit("register_success", data);
}
});
});
Is there a way that I can put client.on("register", function (data) { ...
in one file and client.on("login", function (data) { ...
in another?
Upvotes: 8
Views: 8879
Reputation: 588
Create a listener folder, put all event handlers in separate files, and then with some tricks you can include all that listeners.
in listener/index.js :
module.exports = (io) => {
const fs = require('fs');
const path = require('path');
const listenersPath = path.resolve(__dirname);
io.on('connection', (socket) => {
fs.readdir(listenersPath, (err, files) => {
if (err) {
process.exit(1);
}
files.map((fileName) => {
if (fileName !== 'index.js') {
require(path.resolve(__dirname, fileName))(io, socket);
}
});
});
});
};
and then in different files, you can handle listeners.
listener/login.js:
module.exports = (io, socket) => {
socket.on('login', (data) => {
// handle the event
});
}
in your app.js file:
const initListeners = require('./listeners/index');
const server = app.listen(3000);
const io = require('socket.io')(server);
initListeners(io);
you can also do this for the event emitters.
Upvotes: 2
Reputation: 449
it is very simple
// export from your handler file:
exports.myHandler = socket => data => { // the socket object is available due to closure }
// import in your socket app.js file
import { myHandler } from './handlers/mySocketHandler'
// now just call the myHandler function.
// pass the socket object from the app.js file and it will return a handler function
socket.on('message', myHandler(socket))
Upvotes: 1
Reputation: 29221
I usually split various client related functionality (I usually call them handlers) into individual modules, and then require
and use them in whatever file creates the socket.io connection.
Here is an example module, that exports a function which expects to be passed a socket.io client:
/* register-handler.js */
module.exports = function (client) {
// registration related behaviour goes here...
client.on('register', function (data) {
// do stuff
});
};
Which is consumed by a file that creates a new socket, listens for connections, and passes them to the handler, which then listens to events on the client.
/* main.js */
// require your handlers
var handleRegister = require('./register-handler');
// .. set up socket.io
socket.on('connection', function (client) {
// register handlers
handleRegister(client);
});
Upvotes: 17
Reputation: 13532
Here is one way
socket.on("connection", function (client) {
console.log("Client connected to socket!");
require('./login')(socket, client);
require('./register')(socket, client);
});
login.js
module.exports = function(socket, client) {
client.on("login", function (data) {
validate(data){
socket.sockets.emit("login_success", data);
}
});
};
Upvotes: 3