Is it possible to publish to multiple channels via a single API call?
This capability is not currently available but may be planned for a future API release. If you requirements demand this capability, please contact PubNub Support for further assistance.
Simulate multi-channel publishing with PubNub Channel Groups
Another way to simulate multi-channel publish is to use Channel Groups with a temp channel. It works something like this:
- Every client subscribes to its own unique channel group: cguser1, cguser2, and so on.
- When you want to publish a message to multiple channels, you just need to know who needs to receive the message. I am going to assume you are able to determine this on the server side or pass the user list (UUIDs) to your server. So POST your message to the server (with the list of UUIDs unless you can figure this out on the server).
- Your server then generates a temp channel (use UUID generator if you like) and then iterate through the list of users and add the new temp channel to each of the user's unique channel groups (cg_user1 and such).
- Your server then publishes the message to the new temp channel. Now all channel groups that have that channel will receive the message.
- Once the publish succeeds (on success of the publish) you can then remove the channel from every one of those channel groups. Or, you let the client remove the channel once the client has received the message (might be pulling the message from history or message catchup).
Yes, you are still making multiple calls to PubNub to add the channel to each channel group (might be a add channel to list of channel groups in the future) but what you are saving on is the number of published messages (reduce the message count) from the client.
You could also just POST the message to the server and let the server publish to unique channels (rather channel groups) where each channel is a private user channel. Same result but is multiple publishes with will increase your message counts, if just by a few, unless you have thousands of channels you are publishing to.