Socket.io Uncaught TypeError: Cannot read property 'apply' of undefined

A particular event doesn’t work in my program where I am using socket.io. The rest works fine. This is where the problem occurs:

First html file:

 socket.on('connect', () => {socket.emit('phone', 'phone');});

Server file:

io.on('connection', function(socket){ 
   io.on('phone', function(socket, data){ 
      io.emit('stuurbedrijfsnaam', 'stuurbedrijfsnaam'); 
   }); 
});

2nd html file:

socket.on('stuurbedrijfsnaam', function(socket){
    socket.emit('stuurbedrijfsnaam',bedrijfsnaam) 
})

This is the full error given in the console:

index.js:83 Uncaught TypeError: Cannot read property 'apply' of undefined
at r.emit (index.js:83)
at r.onevent (index.js:83)
at r.onpacket (index.js:83)
at r.<anonymous> (index.js:83)
at r.emit (index.js:83)
at r.ondecoded (index.js:83)
at a.<anonymous> (index.js:83)
at a.r.emit (index.js:83)
at a.add (index.js:83)
at r.ondata (index.js:83)
at r.<anonymous> (index.js:83)
at r.emit (index.js:83)
at r.onPacket (index.js:83)
at r.<anonymous> (index.js:83)
at r.emit (index.js:83)
at r.onPacket (index.js:83)
at r.onData (index.js:83)
at WebSocket.ws.onmessage (index.js:83)

It references index.js:83, which is inside a folder made by socket.io itself. There are lines 81, 82 and 83:

Backoff.prototype.setJitter = function(jitter){
    this.jitter = jitter;
    };

Hope I gave enough resources. It would be cool if someone’s help me out. Thanks!

3 thoughts on “Socket.io Uncaught TypeError: Cannot read property 'apply' of undefined”

  1. I was getting the same error in the console on every hot-reload:

    Uncaught TypeError: Cannot read property 'apply' of undefined
    

    Check your client import statement for socket.io-client

    import io from 'socket.io-client'
    

    should be

    import { io } from 'socket.io-client'
    

    Wasn’t sure how you were importing socket.io based on your html sample, but this is what the socket.io docs say.

    Reply
  2. For Anybody Searching for this error. It can be caused by providing variable as a second parameter in .on method that is undefined. Internal code of socket.io tries to call .apply method to pass arguments into your event handler. If function registered is not existing and therefore undefined, this error occurs since undefined values have no such property . It often may be the case of the typo in the name of the function

    i.e

    socket.on('next_player', this.nextplayer)
    

    should be :

    socket.on('next_player', this.next_player)
    
    Reply
  3. The cause of this error is that you’re trying to call .emit() via the socket argument of your custom event handlers, which is incorrect usage according to the documentation for socket.io client side API.

    Consider revising your client side code as follows by removing the socket argument from your handlers to cause .emit() to be called on the actual socket instance:

    socket.on('stuurbedrijfsnaam', function(){ // remove 'socket' here
    
       // cause emit() to be called on actual socket instance
       socket.emit('stuurbedrijfsnaam',bedrijfsnaam) 
    })
    

    The reason socket.emit('phone', 'phone'); in your first html file is that emit() is called on the original socket instance, rather than via a socket argument passed to the event handler, as you are doing in your seconds html file.

    Hope that helps!

    Reply

Leave a Comment