Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:10:11+00:00 2026-06-14T18:10:11+00:00

So, I have a route function like the following: var http = require(‘http’).createServer(start); function

  • 0

So, I have a route function like the following:

var http = require('http').createServer(start);

function start(req, res){
//routing stuff
}

and below that,I have a socket.io event listener:

io.sockets.on('connection', function(socket){

    socket.on('event', function(data){
        //perform an http response
    }

}

When the socket event ‘event’ is called, I would like to perform an http response like the following:

res.writeHead(200, {'Content-disposition': 'attachment; filename=file.zip'})
res.writeHead(200, { 'Content-Type': 'application/zip' });
var filestream = fs.createReadStream('file.zip');

filestream.on('data', function(chunk) {
    res.write(chunk);
});

filestream.on('end', function() {
    res.end();
});

This last part, when performed within the routing function works just fine, but unfortunately when it is called from the socket event, it of course does not work, because it has no reference to the ‘req’ or ‘res’ objects. How would I go about doing this? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:10:11+00:00Added an answer on June 14, 2026 at 6:10 pm

    Hmmm… interesting problem:

    • It’s not impossible to do something like what you’re trying to do, the flow would be something like this:

      1. Receive http request, don’t respond, keep res object saved somewhere.
      2. Receive websocket request, do your auth/”link” it to the res object saved earlier.
      3. Respond with file via res.
    • BUT it’s not very pretty for a few reasons:

      1. You need to keep res objects saved, if your server restarts a whole bunch of response objects are lost.
      2. You need to figure out how to link websocket clients to http request clients. You could do something with cookies/localstorage to do this, I think.
      3. Scaling to another server will become a lot harder / will you proxy clients to always be served by the same server somehow? Otherwise the linking will get harder.

    I would propose a different solution for you: You want to do some client/server steps using websockets before you let someone download a file?

    • This question has a solution to do downloads via websocket: receive file via websocket and initiate download dialog

      • Sounds like it won’t work on older browsers / IE, but a nice option.
      • Also mentions downloading via hidden iframe

    Check here whether this solution is cross-browser enough for you: http://caniuse.com/#feat=datauri

    Another option would be to generate a unique URL for the download, and only append it to the browser’s window (either as a hidden iframe download, or as a simple download button) once you’ve done your logic via websocket. This option would be more available cross-browser and easier to code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following route: exports.index = function(req, res){ res.render('index', { title: 'Express' });
I'm using Expressjs w/ node.js and I have a route like this: users.init(app.db, function()
I have a route like following, ideally I would like it to match: domain.com/layout/1-slug-is-the-name-of-the-page
I have the following code app.js var express = require('express'), fs = require('fs'), format
I have a module like the following defined in one file define(['mod1', 'mod2'], function
I would like to get URLs like http://mysite.com/user/1 to route to the function get($id)
I have a route defined like this: GET /question/:q_id controllers.Questions.viewQuestion(q_id: Long) Then in my
I have a route that is suppose to match something like this localhost:3000/site/admin and
I want to have a route that looks something like: www.abc.com/companyName/Controller/Action/Id However, all the
I have the following route: {language}/{controller}.mvc/{action}/{id} Once a user has choosen the language it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.