The community forums are in BETA and closed to the public.

How do you subscribe to a channel created on runtime?

Let's say we two kinds of users, User A kind and User B kind and a server.

One of User B kind creates a dynamic channel based on time e.g '00:11' and its passed to the server. The server selects one of User A kind and it needs to tell that particular User A to subscribe to '00:11'. How can this be done?

One method I can think of is that all kinds of User A are subscribed to a channel 'server' and server broadcasts a message containing which User A to select and which channel it should subscribe to.

It seems inefficient as all but one message will be sent to the wrong User A kinds. So how can this be done in a better way? Can the server send a push notification without a requiring a channel to the particular User A?

You can create a single channel for each user, and then send the new channel to specific users.

Thanks for the reply Ian, how can I send the channel to specific users?

Have each user subscribe to a channel using pubnub.subscribe, and then send a message to that channel. Typically you'll use the user's user_id or username from your own backend so it's predictable.

Javascript example:

pubnub.subscribe({
  channel: 'user_id_1234',
  message: function(){}
});
pubnub.publish({
  channel: 'user_id_1234',
  message: "test"
});

Comments to this discussion are now closed!