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

  • Home
  • SEARCH
  • 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 6244153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:16:30+00:00 2026-05-24T12:16:30+00:00

Is it possible to write code that wrap any node.js i/o-callback with my own

  • 0

Is it possible to write code that wrap any node.js i/o-callback with my own function?
(When I say i/o-callback function I mean function that get invoked after some i/o action)

Why do I need such a thing? example: I have a socket.io server
I have bunch of global functions that I am actually getting as an input (I can’t change it, not even reading it, I just paste it as is in my code)

the callback of socket.io.connect(socket,…callback ) interact with those global functions. now upon new connection we set access to the current socket in a global level.
the programmers who wrote those global function are aware of the currentSocket variable and uses it..

The problem is that after calling some async i/o function, some other user/socket might connect and change the currentSocket, later when the async i/o function will get executed the currentSocket will be equals to a different user/socket rather than the original user/socket that invoked that async-function

I thought maybe I can somehow auto-wrap the i/o callback and closure the currentSocket variable, yet I am not sure how to do that…
any idea?

var currentSocket = undefined;
io.sockets.on('connection', function (socket) {
  currentSocket = socket;
  socket.on('msg', function (data) {
    global[data.functionToInvoke];
  });
});

//global function that I can't change or even analyze/read
//========================================================
function g1(){
   //might use currentSocket
   //might invoke async stuff like
   fs.readfile(...cb)
}
function g2(){
   //might use currentSocket
   //might invoke async stuff like
   redisClient.get(...cb)
}
  • 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-05-24T12:16:31+00:00Added an answer on May 24, 2026 at 12:16 pm

    If your functions all share the currentSocket global variable (and if you can’t change them), it is not possible to have multiple connections (as long as the functions are async). If you could change them it would be best just to pass the socket as first parameter to your functions. Another way is to move the definition of currentSocket and your functions inside of the io.sockets connection callback. In this case the currentSocket variable forms a closure and everything should work as expected:

    io.sockets.on('connection', function (socket) {
      var currentSocket = socket;
      socket.on('msg', function (data) {
        global[data.functionToInvoke];
      });
    
      //global function that I can't change or even analyze/read
      //========================================================
      function g1(){
         //might use currentSocket
         //might invoke async stuff like
         fs.readfile(...cb)
      }
      function g2(){
         //might use currentSocket
         //might invoke async stuff like
         redisClient.get(...cb)
      }
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to write code in a Flex application that will only be
If it possible to write byte code for a method that is supposed to
Is it possible to write code which generates a regular expression or XPath that
Even though it is possible to write generic code in C using void pointer(generic
Is it possible to write a doctest unit test that will check that an
Is it possible to write a regular expression that matches a nested pattern that
Would it be possible to write a class that is virtually indistinguishable from an
It's possible to write Markdown content with invalid syntax. Invalid means that the BlueCloth
Is it possible to write a template that changes behavior depending on if a
Is it possible to make code in actionscript-3 to parse the virtual directories that

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.