Lovepreet Singh Batth
Lovepreet Singh Batth

Reputation: 389

Cometd how to make and subscribe channel dynamically?

I am trying to create a very simple cometD app. Mainly i want to create channels dynamically. First i tried localsession channels and after creating subscribed them via client side javascript. It works perfect but not for dynamic channel creation. Let i passed rom variable via javascript ROOM: 'firstroom' this will create a channel like "/members/firstroom".

But on another call from another user like ROOM: 'anotherroom' It creats another room but destroyed old room.

In simple words i want all dynamically created channels stored in memory. Client session i tried:

ClientSessionChannel channel = _session.getLocalSession().getChannel('/members/'+room);
channel.publish(members);

Then i tried to create and subscribe on server like this:

ServerChannel channel = _bayeux.getChannel("/member"+room);
LocalSession localSession = _session.getLocalSession();

channel.subscribe(client);
ServerMessage.Mutable forward = _bayeux.newMessage();
forward.setData(_members);

channel.publish(localSession, forward);

But i thinking how server side subscribtion will change cliend side html code. how

channel.publish(localSession, forward);

will replace

$.cometd.subscribe('/members', manage_members);

where manage_members goes in server side subscribtion.

Straight forward How to create and store channels dynamically?

Upvotes: 0

Views: 1692

Answers (2)

Dennis
Dennis

Reputation: 3731

If you are creating channels dynamically from the server side you should look at the createIfAbsent method of the BayeuxServer class. That allows you to specify the channel path and create (and initialise) the ServerChannel if it is absent.

Upvotes: 0

sbordet
sbordet

Reputation: 18597

I am not sure I understand this question completely, but if the client knows the dynamic part of the channel, you can easily subscribe from JavaScript via:

cometd.subscribe('/members/' + roomName, function(message) { ... });

See the CometD JavaScript documentation about subscription.

If the dynamic part is computed on server side, then your client - in order to receive messages, must subscribe to /members/* and filter out messages for unwanted rooms. Alternatively, the server should send a message to the client telling it what is the exact channel to subscribe to.

Upvotes: 1

Related Questions