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();
};
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 fromconfig/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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With