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.