Reputation: 4334
Context:
We are working on a chat application using PubNub for real-time communication.
PubNub allows 2000 channels in a single channel group, after that you can subscribe up to 10 groups by connection.
The problem we currently have is that our backend sort of needs to be subscribed to every existing channel (through groups) because we have a Bot that reacts to commands.
We handle this connections in node.js, however when adding channels to a group PubNub does not error when adding more than 2000 channels with channel_group_add_channel
, instead it returns a 200 response, and just takes out one previous channel (not sure which).
Because of this, I need to check whats the current number of channels in a group, and if its full - start a new group.
The problem:
We can get multiple simultaneous user registering, meaning new channels added that our bot needs to subscribe to.
Pseudo code to check if we can add more channels to the current group.
addChannel(channel) {
group.canAdd()
.then(group.add(channel));
}
canAdd
would use channel_group_list_channels
to check how many channels are currently in the group. Because of the async nature of javascript we have a race condition:
addChannel
twicechannel_group_list_channels
could return 1999 channelsIs there a way we can workaround this without degrading performance?
Note that even having a counter in something like Redis would have the same issue, since the problem is concurrency.
Upvotes: 4
Views: 462
Reputation: 3397
Our team ended up to refuse to use PubNub channels because of the same reason (and couple of others).
Our brief story:
We have NodeJS component which use PubNub. Over the time our component subscribe to more and more PubNub channels (around 400 per day). After 1 - 2 days we had to restart our component because it stopped to work. It turns out one PubNub instance just stopped to work after some amount of channels. So we looked at PubNub channels and we faces with same race condition issue. Also we discovered strange behaviour of PubNub groups SDK. For example you cannot add more than 1800 channels in one call, you have to do 2 calls (1700 in first and + 300 in second, for example). Wtf? Docs says you can add up to 2000 channels.
Instead we implemented pool of PubNub instances, each described up to 100 channels. And it works perfectly. This is not recommended approach, but what else should you do in the lack of options?
Upvotes: 0
Reputation: 4738
The real question was about subscribing to thousands of channels on your server but Functions was brought up as the solution which prompted the asker to mention the use of chatbots.
Long overdue answer for this question... PubNub Functions have been around for over a year now and building a chatbot is simple when you can tap into any chat bot services: IBM Watson, Gupshup and so many others out there.
PubNub Functions allow you to tap into the message and alter it, encode it, reject it, redirect, whatever you need to do to it, as the message is flying through the PubNub Network without the requirement of hitting your server(s). You simply implement some code in a Function module that will be executed within the PubNub Network based on different messaging events: publish, presence and you can even setup endpoints.
For more resources on creating chatbots:
Upvotes: 1