Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
How to access static method from ExpressJS App Server

I have a server built with ExpressJS. Now, I wish to access my socket io instance from anywhere in my app after initialization, so I tried keeping it in a static method.

But, whenever I invoke the static method I keep getting this error:

AppServer.getServerSocket is not a function

Here is my code:

const ServerSocket = require('./sockets/server_socket');

class AppServer {

    constructor() {
        this.initServerSocket(server);
    }
    
    initServerSocket(server) {
        this.serverSocket = new ServerSocket(server);
    }

    static getServerSocket() { //Whenever I invoke this I keep getting the error: AppServer.getServerSocket is not a function
        return this.serverSocket;
    }
    
    start() {
        const port = process.env.PORT || 8000;
        server.listen(port, function () {
            console.log(`Server running on port ${port} in ${app.get("env")} mode`);
        });
    }
}


let appServer = new AppServer();
appServer.start();

module.exports = AppServer;

Here is how I tried accessing it in another file

...
const AppServer = require('../server');

function emitEvent(event){
  AppServer.getServerSocket().io.emit(event); //Error, AppServer.getServerSocket is not a function
}

How can I resolve this, please?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!