Search code examples
stronglooppubnubloopback4

How to integrate pubnub in loopback4


I am trying to integrate pubnub into my loopback 4 application but I'm not sure where it is supposed to be instantiated or used? Is it supposed to be used in the repository, controller or model? If I try to instantiate it in the repository class, I have to make a pubnub property in the model. Can it be instantiated outside of the class or will that eventually lead to errors?


Solution

  • PubNub Instance Scope

    In general, PubNub should be instantiated as a singleton (once and only once for the life of the application) at the app scope that is accessible by any view, controller, model or utility class.

    Or design it in a way that the PubNub instance is passed around as an argument from/to each of these components in your app. But I don't overthink it too much at this early stages of a PoC effort.

    You can also think of PubNub as database connection:

    • select = subscribe or fetchMessages/history
    • insert & update = publish
    • delete = deleteMessages

    Don't take that analogy too literally but it is a good one for understanding where PubNub fits into your application with respect to your use case and requirements.

    In fact, you should configure your PubNub Account with Apps and Key Sets with the Database mindset. Read this: Can my PubNub account support multiple environments? It goes further with the database analogy with respect to dev, test, prod environments.

    That should provide the insights you need to design your PubNub applications in best practices fashion.