5

I'm working in node JS and I'm trying to send an array, with my clients, from the server to every client with a normal emit, but it keeps giving me this error:

data = JSON.stringify(ev);

TypeError: Converting circular structure to JSON

Shortly, this is what I do.

var clients = new Array();
io.sockets.on('connection', function(socket) {
   clients.push(socket);

   socket.on('loginUser', function(data){
   io.sockets.emit("getUsers", clients);
});

I've seen a couple of other people having this problem, but all those answers didn't work out for me.

Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
  • 1
    possible duplicate of [JSON.stringify, avoid TypeError: Converting circular structure to JSON](http://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json) – mpm Apr 20 '14 at 16:09
  • Tx for your answer but I have no idea how that solution could fix my problem... I'm kind of novice with NodeJS, JSON, etc. – Matthijs Vliegenthart Apr 21 '14 at 20:18

1 Answers1

0

Looking at the bigger problem, you can't just send an array of sockets to the client side. Sockets are objects which only make sense in their current context/process. If you want to control the sockets from client side, I just instead add some sort of RPC functionality.

Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115