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 8238861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:57:18+00:00 2026-06-07T19:57:18+00:00

I have one module which wraps the Socket.io functionality my app is using which

  • 0

I have one module which wraps the Socket.io functionality my app is using which looks something like this:

// realtime.js
var io = require('socket.io'),
    sio;

exports.init = function(expressServer) {
    sio = io.listen(expressServer);
}

...

The main app.js file looks like

// app.js
var rt = require('./realtime.js'),
    other = require('./other.js');
...
rt.init(expressServer);

The other module also uses rt.js

// other.js
var rt = require('./realtime.js');
...

My question is, will both other.js and app.js have the same instance of rt.js?

The answer on SO relating to redis lead me to believe the above statement is true, but in the documentation here it says

Multiple calls to require(‘foo’) may not cause the module code to be
executed multiple times. This is an important feature. With it,
“partially done” objects can be returned, thus allowing transitive
dependencies to be loaded even when they would cause cycles.

which seems to imply that it’s not guaranteed to be the case?

Finally this question appears to indicate it depends on filename and that since there is only one instance of rt.js it shouldn’t be executed more than once. If that’s the case does it depend only on rt.js being the same file or does it depend on the path specified by require. Basically if rt.js and other.js were in lib/, and app.js was one level down the requires in other.js and app.js would point to rt.js from different files, does this matter?

I’d be grateful if anyone could clear this confusion up for me!

  • 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-07T19:57:21+00:00Added an answer on June 7, 2026 at 7:57 pm

    modules are currently evaluated only once, but you should not rely on this. Having any state in module is considered bad practice. What prevents you from passing reference to sio to other.js?

    //realtime
    var io = require('socket.io'),
    
    exports.init = function(expressServer) {
        return io.listen(expressServer);
    }
    
    // app.js
    var rt = require('./realtime.js'),
        other = require('./other.js');
    ...
    var sio = rt.init(expressServer);
    // now ask other.js to use same sio instance
    other.use_sio(sio);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi module maven project which looks something like: main component_one subcomponent_bob
I have a module which looks like this: package Test; use strict; use warnings;
I have admin module. This module have one master page which has the no
Scenario 1: I have one wrapper Perl script which uses another Perl module and
The socket module in python wraps the _socket module which is the C implementation
I have one module (site) in my zend framework app. What I am trying
I would like to retrieve many 'Access' which have one 'Role' in common. It's
I have play-framework application that uses one module which I've created by play new-module
I have one module written on Java - web service module which accepts request
I have a symfony app that provides REST based services. I have one module

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.