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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:14:23+00:00 2026-06-14T20:14:23+00:00

I am trying encapsule a socket.io instance in a module. I did this but

  • 0

I am trying encapsule a socket.io instance in a module. I did this but it looks a bit messy because i have to inject some dependencies for authentication of the socket transport from express / passport.

My problem is i want to access the socket instance outside of the module like socket.on("newDevice", function (data) {});

The socket instance which i get by the connection event is inside the function and it even may not exist on creation because no connection is established. This simply looks kind of wrong to me. I dont want to inject more and more depencies just because i need them inside of the function scope.

I thought about doing the sio.on('connection', function(socket) {}); outside of the module. And maybe i could do it twice, first inside of the module and then outside but i would create two listeners i guess.

Is there any good practice or pattern how to do this properly?

var io = require('socket.io');

var socket = function (server, sessionStore, cookieParser, authentication)  {

    var sio = io.listen(server);

    // Configure socket.io
    sio.configure(function () {

        // Authorize the socket.io request
        sio.set('authorization', function (data, accept) {
                    // Authorization is done here
        });

    });

    sio.on('connection', function(socket) {

    var lastActionTime = new Date();

    // Get the userId from the session
    var session   = socket.handshake.session;
    var userId    = session.passport.user;
    var sessionID = socket.handshake.sessionID;

    var userdata = null;

    // Deserialize user by the userId
    authentication.deserializeUser(userId, function(err, user) {
              // get the userdata
    });


    socket.on('disconnect', function() {

    });

    socket.on('brightnessChange', function(data) {
        // TODO Do something here device control

        // Broadcast to other devices
        this.broadcast.emit('brightnessChange', data);
    });

}); 


    return sio;
};

module.exports = socket;
  • 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-14T20:14:24+00:00Added an answer on June 14, 2026 at 8:14 pm

    I would suggest, the below, flexibility and scales well. I’ve tried both ways and resulted in using multiple connection events.

    I thought about doing the sio.on(‘connection’, function(socket) {}); outside of the module. And maybe i could do it twice, first inside of the module and then outside but i would create
    two listeners i guess.

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

Sidebar

Related Questions

Trying to figure out how I can do this properly. The print_r looks like
trying to figure out why this is happening - I have an input text
I'm trying to encapsulate some functions from the C socket library into my own
I'm trying to export some data I have (stored in a datatable). Some of
I am trying to fetch data and encode it to JSON. I have this
I am trying to write a program where I have to call some functions
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
I'm trying to encapsulate some older win32 code in a C++/CLI ref class to
I'm trying to encapsulate the C++ Standard Library's vector class using templates but I
I'm trying to write some Java code for a set of enum classes. Each

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.