socket.send outside of io.sockets.on( )
Solution 1
I think that this will successfully solve your problem
io.sockets.emit('hello')
Solution 2
Improving previous answer from @Nyxynyx
io.sockets.emit('hello')
It is equivalent to:
io.of('/').emit('hello');
If you need a different route, change the of
function parameter.
Documentation: https://socket.io/docs/v3/server-api/index.html#server-sockets
Related videos on Youtube
Nyxynyx
Hello :) I have no formal education in programming :( And I need your help! :D These days its web development: Node.js Meteor.js Python PHP Laravel Javascript / jQuery d3.js MySQL PostgreSQL MongoDB PostGIS
Updated on June 06, 2022Comments
-
Nyxynyx almost 2 years
I have a loop that querys a database continuously. When the query returns a result, the node.js app will send a message to every client connected to the node server via socket.io v0.8.
Problem:
io.sockets.broadcast.send('msg')
is called in the middle of asetInterval()
loop so it is not within anio.sockets.on()
's callback function and thus this will not work. Whenio.sockets.send('msg')
is used, no message seems to be sent to the client.Node.js code
setInterval(function() { util.log('Checking for new jobs...'); dbCheckQueue(function(results) { if (results.length) { io.sockets.broadcast.send('hello'); } }); }, 10*1000);
However, if the
setInterval
is to be called from withinio.sockets.on('connection',..)
, every connected client will create an additional loop!Node.js code
io.sockets.on('connection', function(socket) { setInterval(function() { util.log('Checking for new jobs...'); dbCheckQueue(function(results) { if (results.length) { io.sockets.send('hello'); } }); }, 10*1000); });
Clientside JS
socket.on('hello', function() { console.log('HELLO received'); })
*How can I get a SINGLE loop to run, but still be able to send a message to all connected clients?
-
Eliasdx over 12 years
io.sockets
can be accessed outside ofio.sockets.on
. -
Nyxynyx over 12 years@Eliasdx When I use
io.sockets.broadcast.send('hello')
outside, I get the errorTypeError: Cannot call method 'send' of undefined
-
Eliasdx over 12 yearsyou can only use
broadcast
on asocket
, e.g.socket.broadcast.send
within aio.sockets.on
-callback. however i think you can send to all clients withio.sockets.send
(withio.
and withoutbroadcast
). -
Nyxynyx over 12 yearsIt appears that none of the clients can receive it if I use
io.sockets.send('hello')
-
Eliasdx over 12 yearsHum. if you want to use
socket.on('hello',...)
you must usesocket.emit
instead ofsocket.send
. -
Nyxynyx over 12 yearsIf I want to send a message to all clients outside of the
io.sockets.on
callback function, isio.sockets.send()
the only function I can use? If so, what should I use on the client side instead ofsocket.on('hello', ...)
-
Eliasdx over 12 yearsJust use
io.sockets.emit('hello')
to send the msg. socket.io/#how-to-use -
Nyxynyx over 12 years@Eliasdx Awesome, that works.
-
Deepak M over 6 yearsCould you tell me how & where to put the
io.sockets.emit('some_link')
in my question - https://stackoverflow.com/questions/45435498/how-to-use-socket-ioemit-function-outside-of-socket-function-nodejs-express ?
-
-
Hossein over 9 yearsThanks! you saved my day.
-
Kyle Corbin Hurst over 2 yearsFor anyone in the future:
io.of('/').emit('hello');
is actually the better function to use if you're using the redis adapter and rooms:io.of('/').adapter.remoteJoin(user.socket_id, data.conversation.id);
. The reason being is thatio.sockets
will emit for every person that joins a room.