Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Keeping socket open after HTTP request/response to Node.js server

Keeping socket open after HTTP request/response to Node.js server


To support a protocol (Icecast Source Protocol) based on Http, I need to be able to use a socket from Node.js's http.Server once the HTTP request is finished. A sample request looks like this:

Client->Server: GET / HTTP/1.0
Client->Server: Some-Headers:header_value
Server->Client: HTTP/1.0 200 OK

This is to support the source of an internet radio stream, the source of the stream data being the client in this case.

Is there any way I can use Node.js's built in http.Server? I have tried this:

this.server = http.createServer(function (req, res) {
    res.writeHead(200, {test: 'woot!'});
    req.connection.on('data', function (data) {
}).listen(1337, '');

If I telnet into port 1337 and make a request, I am able to see the first couple characters of what I type on the server console window, but then the server closes the connection. Ideally, I'd keep that socket open indefinitely, and take the HTTP part out of the loop once the initial request is made.

Is this possible with the stock http.Server class?

Problem courtesy of: Brad


My solution to this problem was to reinvent the wheel and write my own HTTP-ish server. Not perfect, but it works. Hopefully the innards of some of these stock Node.js classes will be exposed some day.

Solution courtesy of: Brad


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Keeping socket open after HTTP request/response to Node.js server


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription