Sails 0.11. Server Side Socket Trap

According to the new version of sails v0.11, the onConnect function is deprecated in the config / sockets.js file. I could not implement socket.on events or raise socket events from the server side. Is there any way to implement?

works by updating the code in config/bootstrap.js , as suggested by @mikermcneil:

 module.exports.bootstrap = function(cb) { sails.io.on('connect', function (socket){ socket.on('testE', function(data) { socket.emit('testEvent',{p1:'hehe'}); }); }); cb(); }; 
+5
source share
1 answer

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

onConnect callback

TL; DR;

Remove the onConnect function with config/sockets.js .

The onConnect lifecycle onConnect deprecated. Instead, if you need to do something when a new socket is connected, send a request from a recently connected client to do this. The goal of onConnect has always been to optimize performance (eliminating the need to make this initial extra round with the server), but using it can lead to confusion and race conditions. If you desperately need to eliminate the server callback, you can bind the handler directly to sails.io.on('connect', function (newlyConnectedSocket){}) in your bootstrap ( config/bootstrap.js ). However, please note that this is not recommended. If you do not encounter true performance issues, you should use the strategy mentioned above for your connectivity logic (that is, send an initial request from the client after the socket is connected). Socket requests are lightweight, so it doesn't add any tangible overhead to your application, and it helps make your code more predictable.

+4
source

Source: https://habr.com/ru/post/1210523/


All Articles