Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

cannot emit sIo.sockets.clients()

cannot emit sIo.sockets.clients()

Problem

It seems that socket.io cannot send the list of connected users, like this:

socket.emit('users', sIo.sockets.clients());

It gives me the following error:

/Users/enrico/Desktop/helloExpress/node_modules/socket.io/lib/parser.js:75
data = JSON.stringify(ev);
              ^
TypeError: Converting circular structure to JSON

Apparently it cannot stringify the returned value from sIo.sockets.clients() Any ideas on how to fix this? Thanks in advance.

Problem courtesy of: john smith

Solution

Since the problem is a circular reference there no real 'fixing it'. Circular reference means that some object in the structure points to another part of the object, making an infinite loop. What you can do is something like this.

var returnList = [];
var clients = sIo.sockets.clients(); // grab list of clients, assuming its an array
for(var i = 0; i 

With this code you can cherry pick the values you want and send only those. This is good for several other reasons. Since this clients list is likely an internal implementation is might also send information about other clients connection.

This is all also pretty speculative as I'm not 100% what libraries you're using, looks like Socket.IO but I cannot find any socket.clients() method.

Solution courtesy of: Morgan ARR Allen

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

cannot emit sIo.sockets.clients()

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×