1

I am trying to pass the contents of io.sockets.clients() through to the client with the following code:

var room_users = io.sockets.clients(newroom);
socket.emit('update_room_users', room_users);

(newroom is a string var)

But I get the error:

Converting circular structure to JSON

in the console.

I have no idea why this is happening as this should be the correct code according the the socket.io site?

Paul Canning
  • 73
  • 1
  • 7
  • But `room_users` is not a string, it's an object ? – adeneo Nov 25 '13 at 17:20
  • possible duplicate of [Chrome sendrequest error: TypeError: Converting circular structure to JSON](http://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json) – Explosion Pills Nov 25 '13 at 17:21
  • Could you link to what code from the socket.io site you're using as a base? Do you simply mean that you're using their `emit` code, or do you mean they produce a result from `io.sockets.clients()` that doesn't have a circular reference? – apsillers Nov 25 '13 at 17:26

1 Answers1

2

You can't convert to JSON an object which contain a circular reference. A circular reference is an object what containt itself.

Example of circular reference:

var a = {};
var b = {a:a};
a.b = b;

room_users certainly contain a circular reference because it contain the socket clients which refer the other clients which refer themselves...


Sorry for my bad english.

Techniv
  • 1,967
  • 15
  • 22
  • I thought "io.sockets.clients()" was just an object with client data in it? I understand the circular reference, but I cannot see how it apparent here... Stupid brain! But it seems that you cannot pass "io.sockets.clients()" at all through to the client (yes, because of the circular reference) but I just want to know why exactly?! – Paul Canning Nov 26 '13 at 10:16
  • What you whant understand exactly ? Why you can't pass a circular reference or why `io.sockets.clients()` contain a circular reference ? – Techniv Nov 26 '13 at 13:42
  • The later, then because of that, the former :) – Paul Canning Nov 26 '13 at 13:47
  • More abstractly, passing the server side client object to a client doesn't make sense. It contain all client-server connection references of itself and other clients. This is not a realy good idea for the security. Why do you whant pass this object ? – Techniv Nov 26 '13 at 14:04
  • To answer you question. In the Socket.IO API you can't send message to the other clients from a client object with the property `broadcast`. To do this, the client object need to has a reference on its associated server. And the server containt the reference of all clients. Current client too. And each clients containt reference of the server. This is an infinit loop. ;-) – Techniv Nov 26 '13 at 14:16
  • I am looking into finding out which other clients are in the same room as the user, so they only see a user list of those in the same room :) – Paul Canning Nov 26 '13 at 14:20
  • Ok. I think the full client objet contain a lot of useless information... ^^ You need to construct a `room_users` object server side, with the specific data needed client side, and only transfert this object. It's better for the network. ;-) You can also create this object before, and maintain it on client's `connect` and `disconnect` to not have to build it each time. – Techniv Nov 26 '13 at 14:53
  • Yea, I slowly figured that out after realising I can't pass this whole object :P Back to the drawing board! – Paul Canning Nov 26 '13 at 15:05