question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

socket.emit inside socket.on function

See original GitHub issue

Note: for support questions, please use one of these channels: stackoverflow or slack

You want to:

  • report a bug
  • request a feature

Current behaviour

invoke a socket.emit inside a socket.on function does not work. (see code below) socket.on('notification', function (data) { socket.emit('news', { hello: 'world' }); });

Steps to reproduce (if the current behaviour is a bug)

Server side, emit a message from inside a “socket.on” method.

Expected behaviour

Message sent to client

Setup

  • OS: Archlinux
  • browser: Firefox
  • socket.io version: 1.7.2

Other information (e.g. stacktraces, related issues, suggestions how to fix)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:22 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
keevitajacommented, Oct 19, 2017

use io.sockets.emit('news', message) instead of socket.emit

const app = require('http').createServer()
const io = require('socket.io')(app)

io.on('connection', function (socket) {
    socket.on('broadcast', (message)=> {
        const token = socket.request.headers.authorization.replace('Bearer ', '')

        if (token !== '12b3c4d5e6f7g8h9i') {
            return
        }

        // socket.emit('news', message) will not work
        io.sockets.emit('news', message)
    })
})

app.listen(8080)

I just started with socket.io and i have no clue if this is the right way to do this. So perhaps some more seasoned socket.io guru can help us.

2reactions
kyleshevlincommented, Feb 7, 2017

Sorry that my above code isn’t clear. I am actually sending a socket.emit('send:action') from another client. The server receives the ‘send:action’ event and should emit the dispatch:action event. I can confirm that my client is emitting properly and that my server is responding, as I mentioned the console.log(action) is working, but the nested socket.emit() is not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

socket.io emit doesn't work inside socket.on - Stack Overflow
I know the 'fromClient' side is working cause it does print the value of data, but it doesn't print the 'fromServer' value. I'm...
Read more >
Emitting events | Socket.IO
IO API is inspired from the Node.js EventEmitter, which means you can emit events on one side ... This also works in the...
Read more >
Socket.IO - Event Handling - Tutorialspoint
We can also emit events from the client. To emit an event from your client, use the emit function on the socket object....
Read more >
How to use Socket function in io - Javascript - Tabnine
Most used socket functions · Socket.on · Server.on. The event fired when we get a new connection · Socket.emit · Socket.id. The unique...
Read more >
Introduction to Sockets.IO in Node.js - GeeksforGeeks
Introduction to Sockets.IO in Node.js · Reliability: It can establishes connection even in the presence of proxies, load-balancers, personal ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found