Socket.io: Socket.io threw err:fn.bind is not a function

Created on 4 Feb 2016  ·  18Comments  ·  Source: socketio/socket.io

why does it happen and how can I fix it ? please help~

err from where:
...\socket.io-adapter\index.js:195
if (fn) process.nextTick(fn.bind(null, null, sids));

socket.io version:
1.4.5

Most helpful comment

I checked the example in the book NodeJs in action, and of course it will not work if you use the latest versions of socket.io, you will have the 2 following lines to get it to work:

From:
var usersInRoom = io.sockets.clients(room);

To:
var usersInRoom = io.of('/').in(room).clients;

and from:
socket.emit('rooms', io.sockets.manager.rooms);

to:
socket.emit('rooms', io.of('/').adapter.rooms);

Greetings!

All 18 comments

I got the same error while trying to build the multiroom chat application from the book "Node.js in action"

<MY_PATH>\chatrooms\node_modules\socket.io-adapter\index.js:195
  if (fn) process.nextTick(fn.bind(null, null, sids));
                              ^

TypeError: fn.bind is not a function
    at Adapter.clients (<MY_PATH>\chatrooms\node_modules\socket.io-adapter\index.js:195:31)
    at Namespace.clients (<MY_PATH>\chatrooms\node_modules\socket.io\lib\namespace.js:253:16)
    at joinRoom (<MY_PATH>\chatrooms\lib\chat_server.js:71:32)
    at Namespace.<anonymous> (<MY_PATH>\chatrooms\lib\chat_server.js:20:5)
    at emitOne (events.js:90:13)
    at Namespace.emit (events.js:182:7)
    at Namespace.emit (<MY_PATH>\chatrooms\node_modules\socket.io\lib\namespace.js:206:10)
    at <MY_PATH>\chatrooms\node_modules\socket.io\lib\namespace.js:174:14
    at _combinedTickCallback (node.js:370:9)
    at process._tickCallback (node.js:401:11)

The issue appears if you update the dependencies to the latest versions:

  "dependencies": {
    "mime": "^1.3.4",
    "socket.io": "^1.4.5"
  }

while it works if you use the dependencies specified by the book

  "dependencies": {
    "socket.io": "~0.9.6",
    "mime": "~1.2.7"
  }

BTW, before commenting here, I've read the page Migration from 0.9 from the socket.io site but I've not found a solution.

I can upload the simple chat project somewhere if needed (it's very small).

I checked the example in the book NodeJs in action, and of course it will not work if you use the latest versions of socket.io, you will have the 2 following lines to get it to work:

From:
var usersInRoom = io.sockets.clients(room);

To:
var usersInRoom = io.of('/').in(room).clients;

and from:
socket.emit('rooms', io.sockets.manager.rooms);

to:
socket.emit('rooms', io.of('/').adapter.rooms);

Greetings!

thnx @CheckMater

thnx @CheckMater

thnx @CheckMater

thnx @CheckMater

thnx @CheckMater

Thanks man @CheckMater

thanks @CheckMater

thanks @CheckMater

thanks @CheckMater

thanks @CheckMater

Thanks @CheckMater . But for me simply changing var usersInRoom = io.of('/').in(room).clients; didn't worked. As this clients is a function which takes a callback as a param, below snippet worked for me.

io.of('/').in(room).clients(function(error,clients){
        var otherUsers = [];
                for(var i in clients){
                        if(socket.id != clients[i]) otherUsers.push(nickNames[clients[i]]);
                }
                var userSummary = 'Users Currently in ' + room + ' are : ' + otherUsers.join(', ') + '.';
                socket.emit('message',{text:userSummary});
    });

console.log(io.of('/').in(room).clients);

ReferenceError: rooms is not defined : at findClientsSocket

thnx @CheckMater

thnx @CheckMater

thx @CheckMater

thanks @CheckMater

Was this page helpful?
0 / 5 - 0 ratings

Related issues

doughsay picture doughsay  ·  4Comments

varHarrie picture varHarrie  ·  3Comments

shashuec picture shashuec  ·  4Comments

thebinarypenguin picture thebinarypenguin  ·  4Comments

Elliot9 picture Elliot9  ·  4Comments