-1

Using rx-node library, I expect this code to work. But it doesn't recognize a socket as a stream,

stream.pause();
          ^

TypeError: Cannot read property 'pause' of undefined

even though the net documentation appears that it is.

How do I get the stream portion of a net.createServer()

var RxNode = require('rx-node');
var server;
var sSocket;

server =  net.createServer(function (socket) {
  sSocket = socket;
});

var subscription = RxNode.fromStream(sSocket, 'ondata')
    .subscribe(function (x) {process.stdout.write("Got data " + x + "\n"); });

server.listen(xxxxx, 'xxx.xxx.xx.xxx');
Ivan
  • 7,448
  • 14
  • 69
  • 134
  • 1
    I'm not familiar with rx-node, but I would assume that `var subscription = ...` needs to be in `net.createServer` callback function, otherwise `sSocket` is still an undefined variable by the time that code runs. – Jekrb Mar 13 '17 at 03:04
  • 2
    it's not about "recognition", the error clearly states there is no such defined variable at the moment, you need to either fix order of your code or declare variable – Etki Mar 13 '17 at 03:12
  • Jekrb you are right. – Ivan Mar 13 '17 at 03:13
  • How do I rearange my code so that subscription can be outside createServer? – Ivan Mar 13 '17 at 03:19
  • 1
    @Ivan: You can't. You just need to get used to it. – slebetman Mar 13 '17 at 03:22
  • 1
    @Ivan: Almost all questions that ask "how can I write my code outside of the callback" will be marked as duplicate to this question: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call. Those that are not will be marked as duplicate to other similar questions. – slebetman Mar 13 '17 at 03:24
  • Something doesn't make sense. Then how do I make an observer of an observable in a different module ? – Ivan Mar 13 '17 at 03:46

1 Answers1

0

Yes, socket is a stream. The problem you get is not that it is not a stream but that it is undefined. See the docs:

Class: net.Socket# Added in: v0.3.4 This object is an abstraction of a TCP or local socket. net.Socket instances implement a duplex Stream interface. They can be created by the user and used as a client (with connect()) or they can be created by Node.js and passed to the user through the 'connection' event of a server. [emphasis added]

When this:

stream.pause();

gives you this error:

TypeError: Cannot read property 'pause' of undefined

it means that stream is undefined, not that it is not a stream.

In your case the stream is most likely the sSocket variable that you pass to RxNode.fromStream() which is obviously undefined just by looking at the code. The order of evaluation is something like:

var sSocket; // (1)

server =  net.createServer(function (socket) { // (2)
  sSocket = socket; // (4)
});

var subscription = RxNode.fromStream(sSocket, 'ondata') // (3)

so as you can see you are trying to use it before it gets defined.

Now, how to use it after it is defined? You need to use it inside of the net.createServer() callback, or in some function started after that callback has fired.

Another problem with your code is that the function that you pass to net.createServer() is a connectionListener which is automatically set as a listener for the 'connection' event and you seem to be using it as if it was something else. See the examples in the documentation:

It is pretty much exactly the same as what you are trying to do.

rsp
  • 107,747
  • 29
  • 201
  • 177