Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating socket.io logic from app.js in feathersjs

I currently have the following in configuration for socket.io in my app.js file:

app
.configure(feathers.socketio(function(io){
    io.on('connection', function(socket){
        socket.emit('connect',{test: 'wow'});
        socket.on('createRecord', function(socket){
            analytics.service('record').create({type: socket.name, user: socket.interest}, function(error, user){
            });
        })
    })

}))

The above works fine, but is there a way to separate the socket.io logic from the app.js, as it will get very large as I continue on adding more emit and on methods. I am aware this can be done by passing the socket object into as an argument into a module in another file and using it from there. However, I am not sure how to proceed with that in feathersjs.

like image 794
jumpr3 Avatar asked Apr 25 '26 22:04

jumpr3


1 Answers

If I'm understanding you correctly extracting

module.exports = feathers.socketio(function(io){
    io.on('connection', function(socket){
        socket.emit('connect',{test: 'wow'});
        socket.on('createRecord', function(socket){
            analytics.service('record').create({type: socket.name, user: socket.interest}, function(error, user){
            });
        })
    })

})

to ./socketsConfig and

var socketsConfig = require('./socketsConfig')
app.configure(socketsConfig);

will do the job.

like image 181
sdespolit Avatar answered Apr 30 '26 03:04

sdespolit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!