Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails 0.11 catching socket events from server side

As per the new release of sails v0.11, the function onConnect is deprecated in config/sockets.js file. I couldn't implement the socket.on events or catch socket events from server side. Is there any way to implement ?

working by updating code in config/bootstrap.js as @mikermcneil suggested:

module.exports.bootstrap = function(cb) { 
    sails.io.on('connect', function (socket){
        socket.on('testE',  function(data)  {
            socket.emit('testEvent',{p1:'hehe'});
        });
    });
cb();
};
like image 271
Vivek Bharatha Avatar asked Sep 30 '22 01:09

Vivek Bharatha


1 Answers

check out the migration guide here: https://github.com/balderdashy/sails/blob/master/0.11-migration-guide.md#onconnect-lifecycle-callback

onConnect lifecycle callback

tldr;

Remove your onConnect function from config/sockets.js.

The onConnect lifecycle callback has been deprecated. Instead, if you need to do something when a new socket is connected, send a request from the newly-connected client to do so. The purpose of onConnect was always for optimizing performance (eliminating the need to do this initial extra round-trip with the server), yet its use can lead to confusion and race conditions. If you desperately need to eliminate the server roundtrip, you can bind a handler directly on sails.io.on('connect', function (newlyConnectedSocket){}) in your bootstrap function (config/bootstrap.js). However, note that this is discouraged. Unless you're facing true production performance issues, you should use the strategy mentioned above for your "on connection" logic (i.e. send an initial request from the client after the socket connects). Socket requests are lightweight, so this doesn't add any tangible overhead to your application, and it will help make your code more predictable.

like image 130
mikermcneil Avatar answered Oct 06 '22 19:10

mikermcneil