1

So I have a place in my current project where I am initializing an APN (Apple Push Notification) connection but I need to initialize it one as I do not need more than a single instance of the connection. Currently I have a class that creates the connection and puts our needed listeners on the connection to watch for errors and such. It looks something like this:

export default class pushService {
    constructor(options) {
        ... check that options is proper ...

        this.apnConnection = apn.Provider(options);

        ... add listeners ...
    }

    sendMessage(message, recipents) {
        this.apnConnection.send() messages
    }
}

Now there are potentially multiple locations where this will need to be used, but as I said I'd prefer to have only one connected instance of the APN connection for the whole of the application.

So, is it best to initialize this in the main file of the application and make it a named export, or should it be initiated in some other way? I don't think importing the whole main application file is a great idea if it can be avoided, but maybe there is no issue in doing so?

Also if this is too subjective I am sorry, but this is something I don't think I've seen asked here yet nor answered in my Google searching, and it seems like something that may have one or more good solutions.

firrae
  • 171
  • 1
  • 13
  • Possible duplicate of [node.js global variables?](http://stackoverflow.com/questions/5447771/node-js-global-variables) – tcooc Nov 02 '16 at 20:18
  • @tcooc I don't believe so as the question I am asking is to do with creating an instance of the class I exampled where I would have to input variables on construction? – firrae Nov 02 '16 at 20:29
  • Just create an instance of the class, and do `global.foo = instance;`. – tcooc Nov 02 '16 at 20:39
  • I would point to @DaveDopson's reply to his answer: "Specifically, with GLOBAL, the issue is one of readability. If your program promiscuously uses global variables, it means that in order to understand the code, I must understand the dynamic runtime state of the entire app. This is why programmers are leery of globals. I'm sure there's dozens of ways to use them effectively, but we've mostly just seen them abused by junior programmers to the ill of the product." I guess that more or less sums up why I'm trying to see if there is a better way that is the "best practice"? But maybe that is. – firrae Nov 02 '16 at 20:49
  • Using global is definitely not the best practice. Your question is extremely subjective and deals with software design & dependency injection, so I don't think it's really answerable. A simple solution is to have one "library" module export the class, and another "instance" module export the instance of the class. It might work for you. – tcooc Nov 02 '16 at 21:07
  • @tcooc That's a fair point, I was also pointed to the singleton method (unless that is what you said). I'm looking into that to see if it'll work. – firrae Nov 03 '16 at 15:46

1 Answers1

-2

The best answer I found was based on this blog post here:

ES6 Modules - Single Instance Pattern

In the end my database connection looks like this:

let instance = null;

export class dbConnection {
    constructor() {
        if(!instance)
            instance = dbConnectionLibrary();

        return instance;
    }
}

I plan on doing the same for my push notification services, and possibly extending that dbConnection class to add the connection to an array of connections as we have a new requirement of needing multiple different databases connected on startup.

u-ways
  • 6,136
  • 5
  • 31
  • 47
firrae
  • 171
  • 1
  • 13