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.io Client doesn't (re)emit without page reload

See original GitHub issue

TL;DR; I can connect to the node.js server and retrieve my data when I click on <button>Get Data</button>. But nothing is emitted if I click on that button again.

I’m not sure if this is a bug, but using socket.io 1.0.4 . . .

… on document ready, my client connects to the node socket.io server to get the client .js file:

    /*** Attempt to get the socket.io.js file from the node listener. ***/
    var svrPort = 8072
    var scriptPath = "https://"+location.host+":"+svrPort+'/socket.io/socket.io.js';
    jQuery.getScript( scriptPath, function( data, textStatus, jqxhr ) {
        if (jqxhr.status == 200) {
            listenerUp = true;
            console.debug( "socket.io.js file received." );
        }
    }); 

The page has a <button id='RS_Refresh'>Get Data</button> that has an event trigger defined at:

jQuery('#RS_Refresh').click(function(){
   get_AllRS(true); 
});

The get_ALLRS() function does indeed establish a connection to the node server, and on connect, emits a request for the data to be displayed on the web page, which works perfectly:

/* Some code omitted for brevity */
function get_AllRS(showSpinner) {
    var getAllRS_sock = io.connect(svr,options);
    var toListener =
    {
        'cType'     : 'get_AllRS',
        'DEBUG'     : RS_DEBUG,
        'clientID'  : clientID,
        'userID'    : userID,
    };

   /* On second attempt, this gets executed, but no request is actually 
      emitted  - no traffic seen in Firebug's 'Net' tab */
    if (getAllRS_sock.connected) {
        console.log('emitting. . .');
        getAllRS_sock.emit('get_AllRS',toListener) ;
    }

    /*** When connected, send get_AllRS ***/
    getAllRS_sock.on('connect', function getAllRS_sock_on_connect() {
        console.log('In getAllRS_sock_on_connect()');

        /*** Let's talk to the server ***/
        getAllRS_sock.emit('get_AllRS',toListener) ;
    });

    /*** Mission Accomplished ***/
    getAllRS_sock.on('got_AllRS', function getAllRS_sock_on_got_AllRS(incoming) {
        console.log('In getAllRS_sock_on_got_AllRS()');

        console.debug('=== got_AllRS incoming ======================');
        console.debug(incoming)
        console.debug('=============================================');

        if (typeof(incoming) !== 'undefined'){
            populateRSFields(incoming);
            setRSAlert('Fields Retrieved successfully','flash_success');
        }
        else{
            revertValues();
            setRSAlert('Fields were NOT retrieved','flash_error');
        }

        /* Should I disconnect from the node server?? Doesn't work either way */ 
        // getAllRS_sock.emit('forceDisconnect',{}) ;
        // getAllRS_sock.destroy();
    });
}

However, if I click on the Get Data button again, nothing is emitted, according to the ‘Net’ tab in Firebug and node.js doesn’t hear/see the second request in DEBUG=* mode, … until I refresh the entire web page.

What gives?

Should I emit to the server to disconnect the client (i.e. .emit('forceDisconnect',{})? (The problem seems to persist either way, unless I’m doing it wrong.)

Is my approach incorrect? Or maybe you could show me an example of how you made this work?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
darrachequesnecommented, Apr 22, 2017

@Haideralee please make sure the listener is properly initialized in your Ctrl 1.

Closing, as the initial issue should be fixed by now.

0reactions
Haideraleecommented, Apr 22, 2017

I am facing the same issue but in my case the problem in on section, emit is working fine but the listen is not working as expected. Ex:

Ctrl 1
       this.SocketIo.socket.on('user:updated', function(data){
            console.log("user:updated in choose place", data)
        });

Ctrl 2

       this.SocketIo.socket.on('user:updated', function(data){
            console.log("user:updated in choose place", data)
        });

        this.SocketIo.socket.emit('acceptedJob', {res});

Server

     socket.on('acceptedJob', (res) => {
            socket.log('acceptedJob');
            socket.emit('user:updated', res);
      });

the confusing thing is on running in Ctrl 2 but not listen in Ctrl 1

Read more comments on GitHub >

github_iconTop Results From Across the Web

socket.io "connect" event on refresh/reload wont fire
When I reload the page (cmd+r), none of the client side events fire. The server side recognizes the new connection "sees" the new...
Read more >
Emitting events | Socket.IO
IO API is inspired from the Node.js EventEmitter, which means you can emit events on one side and register listeners on the other:....
Read more >
Everything you need to know about Socket.IO - Ably Realtime
Socket.IO allows bi-directional communication between client and server. ... Inside the function we are using io.emit() to send a message to ...
Read more >
How To Create a Real-Time App with Socket.IO, Angular, and ...
With WebSockets, the server may send data to a client without the ... And two event types that are emitted by our socket...
Read more >
How to Build a Real-time Chat App with React, Node, Socket ...
IO means that the client doesn't have to make multiple polling AJAX ... the client version of socket.io, that allows us to "emit"...
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