Paul Canning
Paul Canning

Reputation: 73

io.sockets.clients() gives error "Converting circular structure to JSON"

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?

Upvotes: 1

Views: 2417

Answers (1)

Techniv
Techniv

Reputation: 1967

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.

Upvotes: 2

Related Questions