socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

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

Open zyf394 opened this issue 10 years ago • 18 comments

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

zyf394 avatar Feb 04 '16 03:02 zyf394

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).

davebaol avatar Mar 05 '16 16:03 davebaol

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!

CheckMater avatar May 10 '16 21:05 CheckMater

thnx @CheckMater

ReactorboY avatar Jul 05 '16 07:07 ReactorboY

thnx @CheckMater

limboinf avatar Aug 16 '16 15:08 limboinf

thnx @CheckMater

zymov avatar Oct 29 '16 16:10 zymov

thnx @CheckMater

GiovanniCst avatar Jan 02 '17 00:01 GiovanniCst

thnx @CheckMater

zyf394 avatar Jan 04 '17 10:01 zyf394

Thanks man @CheckMater

austin047 avatar Jan 14 '17 23:01 austin047

thanks @CheckMater

tvnhan avatar Feb 27 '17 08:02 tvnhan

thanks @CheckMater

SailorMoonS avatar Mar 01 '17 05:03 SailorMoonS

thanks @CheckMater

singlespider avatar Mar 12 '17 10:03 singlespider

thanks @CheckMater

DominicTylor avatar Mar 21 '17 15:03 DominicTylor

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});
	});

thangarajks avatar Mar 29 '17 08:03 thangarajks

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

ReferenceError: rooms is not defined : at findClientsSocket

Santosh-Lodhi avatar Nov 20 '17 10:11 Santosh-Lodhi

thnx @CheckMater

chaoshenr avatar Jan 14 '18 14:01 chaoshenr

thnx @CheckMater

Creekie1337 avatar Jul 16 '18 15:07 Creekie1337

thx @CheckMater

JMTK avatar Aug 15 '18 13:08 JMTK

thanks @CheckMater

ishutrehan avatar Feb 11 '19 12:02 ishutrehan