Reputation: 87783
I want to create a two-way stream between a Node.JS client and a Node.JS server, via an HTTP connection. But it seems that the server's request
event is not called until the client calls req.end()
. How can I establish a two-way communication? Do I need to use separate requests?
Clarification: My expectation was that it was possible for
Edit: OK, here is the summary of the options:
TCP sockets on a separate port: This is good if you don't mind opening a separate port, and then modifying previous use of HTTP (request path and headers, and response headers) to use the stream instead. (I declined this option.)
Separate HTTP requests, ending the request every time you need to flush. Managing the state would be done in the application, because now an ended connection will no longer represent an ended session. (In the short term, I will use this option.)
Socket.IO (which is an implementation of WebSockets + reverse compatibility): This option is very appealing, but you need to add the Socket.IO layer to be in front of the HTTP layer. Socket.IO would pass through all HTTP requests, but WebSocket requests would be handled differently. (In the long term, I will use this option, but I will have to modify my front-end to permit this.)
As I understand it, the Node.JS http
library will not allow me to send a response header, unless the client has already completed (or at least substantially began) sending the request body. This I suppose is for good reason, so this third option can also be titled Add a layer in front of the http
library.
Upvotes: 0
Views: 1965
Reputation: 99533
If you are really looking for a 2-way communication system, HTTP is not really the correct protocol.. The whole idea is that it's stateless and handled 1 request at a time. There's exceptions to this rule, but those are really stretching what HTTP is supposed to do, and could be considered hacks.
It sounds like you should be using something like Socket.IO
Upvotes: 2