pubnub / javascript

PubNub JavaScript SDK docs https://www.pubnub.com/docs/sdks/javascript
Other
553 stars 401 forks source link

How to handle multiple instances in node #254

Open sarink opened 2 years ago

sarink commented 2 years ago

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 commented 2 years ago

@sarink this question is better asked on support@pubnub.com 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 commented 2 years ago

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 commented 11 months ago

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'
  }
}