5

I'm having an issue sending an array from the backend js to the client side.

I've tried the following on the server side:

for (var i=0; i < clients.length; i++) {
    clients[i].send(clients);
}

for (var i=0; i < clients.length; i++) {
    clients[i].send(JSON.stringify(clients));
}
  • also using json.stringify on the client side aswell

for (var i=0; i < clients.length; i++) {
    clients[i].send(clients.join('\n')));
}
  • again I've tried this method on the client side aswell.

Unfortunately, none of the above solutions worked, The JSON.stringify method obviously didn't work on the serverside due to JSON.stringify being a browser method however the other methods returned either [object Object] or "[object Object]"

How can I send the array clients to client side, or even if I can encode it into JSON and then send it over and parse it on the client side.

Really all I need is to send the contents over to the client side, but I have no clue how to do so haha

Any ideas are appreciated :)

4
  • What kind of websockets framework do you use? socket.io or smth. else ? Commented May 20, 2012 at 6:48
  • it's just nodejs and websocket, no other frameworks Commented May 20, 2012 at 6:58
  • 1
    why don't you use sockets ? the idea is in that case you can use room and broadcast to all the users in the room instead of using .send to each client in a for loop Commented May 20, 2012 at 7:04
  • Yep... Also be aware that websocket actually work only client <-> server, you can't connect client <-> client Commented May 20, 2012 at 7:06

2 Answers 2

7

If you are using Nodejs, then the JSON object is available by default (it is built into V8 so Nodejs gets it for free).

The inverse of the JSON.stringify() method is JSON.parse().

For example:

> s = JSON.stringify([1,2,3]);
'[1,2,3]'
> a = JSON.parse(s);
[ 1, 2, 3 ]

If the server is sending the result of stringify, then the client must run parse to extract the original data and vice versa.

Sign up to request clarification or add additional context in comments.

2 Comments

TypeError: Converting circular structure to JSON, however it works fine if I use a single dimensional array
Aha! That error is the key. The clients list that you are trying to serialize to JSON is too complicated and probably has a reference to itself somewhere down the hierarchy. Multidimensional arrays are fine, but if the way you are constructing it somehow refers back to itself somehow then you cannot serialize it (very few serialization formats allow self-reference).
1

Although this isn't a real solution it is working for me for now unless I find a better way to do it.

Using the .toString() method:

    for (var i=0; i < clients.length; i++) {
      clients[i].send(clients.toString());
    }

and then interpreting that output on the client side with this

var clients = string.split(',');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.