Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle multiple instances in node #254

Open
sarink opened this issue Feb 24, 2022 · 3 comments
Open

How to handle multiple instances in node #254

sarink opened this issue Feb 24, 2022 · 3 comments

Comments

@sarink
Copy link

sarink commented Feb 24, 2022

I can't find an answer to this anywhere.

It's recommended to have a different uuid for each user or device. This uuid is used in the constructor function, when calling new PubNub. This means that on your server there will be a zillion pubnub instances? Should creating these be memoized? Do they persist a connection which needs to be closed?

Consider the following code:

export const sendUserNotification = (userId: string, channel: Channel, message: Notification) => {
  const pubnub = new PubNub({
    publishKey: PUBNUB_PUBLISH_KEY,
    subscribeKey: PUBNUB_SUBSCRIBE_KEY,
    uuid: userId,
  });
  return pubnub.publish({ channel, message });
};

is it ok to just new one up whenever you need it? Will it be garbage collected when the function closes, or will this create a memory leak?

@pubnubcraig
Copy link
Contributor

@sarink this question is better asked on [email protected] but in short, if you want to minimize the TCP handshakes, just create the PN instance so that it can be reused rather than reinstantiating every time you need it.

For your server, you could just use the same UUID each time or create multiple instances each with a different UUID. It really depends on your requirements.

@sarink
Copy link
Author

sarink commented Mar 1, 2022

hey @pubnubcraig , thanks for your response, but I'm not sure that really answers my questions.

  1. Does the above code create a memory leak?
  2. When should you use one uuid per user, and when should you use one uuid per server?

@stanyq4
Copy link

stanyq4 commented Dec 18, 2023

Curious about the answer to @sarink question as well.

Because seems like we are getting memory leaks here:

[Error]: PubNub call failed, check status for details
    at new t (/Users/user/Dev/projects/platform/platform-eds/dist/handler.js:16:993036)
    at N (/Users/user/Dev/projects/platform/platform-eds/dist/handler.js:16:995766)
    at /Users/user/Dev/projects/platform/platform-eds/dist/handler.js:16:1177265
    at R.callback (/Users/user/Dev/projects/platform/platform-eds/dist/handler.js:16:1320257)
    at ClientRequest.<anonymous> (/Users/user/Dev/projects/platform/platform-eds/dist/handler.js:16:1319208)
    at ClientRequest.emit (node:events:527:28)
    at TLSSocket.socketErrorListener (node:_http_client:454:9)
    at TLSSocket.emit (node:events:527:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3) {
  status: {
    error: true,
    operation: 'PNPublishOperation',
    errorData: Error: connect ENOMEM 54.175.191.204:443 - Local (0.0.0.0:0)
        at internalConnect (node:net:953:16)
        at defaultTriggerAsyncIdScope (node:internal/async_hooks:465:18)
        at GetAddrInfoReqWrap.emitLookup [as callback] (node:net:1097:9)
        at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:73:8) {
      errno: -12,
      code: 'ENOMEM',
      syscall: 'connect',
      address: '54.175.191.204',
      port: 443,
      response: undefined
    },
    category: 'PNUnknownCategory'
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants