0

I need to serialize the stream object that is the callback from a net.createServer()

var server = net.createServer(function (stream) {

  var json = JSON.stringify(stream);

However, When I do this I get a Type Error because the stream object contains circular attributes.

Is there a workaround for this?

3
  • Why do you want to serialize the stream. You can't just pass a stream around to something else. Commented Apr 22, 2011 at 18:59
  • The server is asynchronous, you need to handle the 'data' event. Check out the stream documentation in the node.js api. Commented Apr 22, 2011 at 19:41
  • The reason I want to put this in JSON format is so that I can store the data in Redis. Commented Apr 27, 2011 at 21:07

1 Answer 1

2

@Jason is correct here. You want to take the data from the stream, not the stream itself, and put it into Redis. In order to do this, you must add event listeners to the stream for the data and end events. In the event handlers you will get a chunk of data with each callback that you can either write to redis in pieces or assemble them in memory and then write the whole thing when the end callback occurs. Here's an example you can follow.

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

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.