cometd sample code for an application which subscribes for more than one channel

54 views
Skip to first unread message

Santh

unread,
May 18, 2013, 2:51:06 PM5/18/13
to cometd...@googlegroups.com
Hi,

I'm trying to develop a system where I need to subscribe for number of channels from the same page through Jquery. I searched for a sample code which does this so that I can use that as the reference. Mainly I want to know how multiple channels are subscribed from the same page while the number of connection to the comet serer is one.

Thanks, Santh

Simone Bordet

unread,
May 18, 2013, 3:34:06 PM5/18/13
to cometd-users
Hi,
In the same way a postman can deliver multiple envelopes to multiple recipients.
Seriously, connection==postman, channels==addresses, messages== envelopes.

If you want the gory details, have a look at the Bayeux protocol that
CometD is using: http://docs.cometd.org/reference/#bayeux.

Or, you may not worry about the details, and just trust CometD to do
the right thing, and you can concentrate on your application.

--
Simone Bordet
----
http://cometd.org
http://webtide.com
http://intalio.com
Developer advice, training, services and support
from the Jetty & CometD experts.
Intalio, the modern way to build business applications.

Santh

unread,
May 18, 2013, 4:02:41 PM5/18/13
to cometd...@googlegroups.com
Hi Simon,

Thanks for that. I can understand that. But I wanted a code sample because when I try in the following way, It is not working properly. I see a disconnection & reconnection when the second channel is added. Pls let me know whether this is correct implementation:

function _metaHandshake(handshake) {
        if (handshake.successful === true) {
            cometd.batch(function () {                
                cometd.addListener(channelName1, function (message) {
                    //processResponse
                });                
                cometd.publish(channelName1);
                
                cometd.addListener(channelName2, function (message) {
                    //processResponse
                });                
                cometd.publish(channelName2);
            });

Simone Bordet

unread,
May 18, 2013, 4:22:23 PM5/18/13
to cometd-users
Hi,

On Sat, May 18, 2013 at 10:02 PM, Santh <suthar...@gmail.com> wrote:
> Hi Simon,
>
> Thanks for that. I can understand that. But I wanted a code sample because
> when I try in the following way, It is not working properly. I see a
> disconnection & reconnection when the second channel is added. Pls let me
> know whether this is correct implementation:
>
> function _metaHandshake(handshake) {
> if (handshake.successful === true) {
> cometd.batch(function () {
> cometd.addListener(channelName1, function (message) {
> //processResponse
> });
> cometd.publish(channelName1);
>
> cometd.addListener(channelName2, function (message) {
> //processResponse
> });
> cometd.publish(channelName2);
> });
> }
> }

Makes little sense to add listeners in a /meta/handshake function:
every time you re-handshake, you will add another listener without
removing the previous ones.
Please have a read at
http://docs.cometd.org/reference/#javascript_subscribe_vs_listen.

You say "I see a disconnection & reconnection when the second channel is added".

You need to be way more specific:
1. define "disconnection and reconnection" (at TCP level ? at Bayeux
level ? Other levels ?),
2. where do you see it (client ? server ?),
3. there is no "channel added", so I have no idea what you refer to.

You can't post a snippet of code saying that in your opinion is not
working properly, without posting the eventual error log, or at least
what behaviour you expect, and what you actually get.
This is a very similar snippet of code, and it works:
https://github.com/cometd/cometd-tutorials/blob/master/server-stock-price/src/main/webapp/application.js.

This read may help you: http://www.catb.org/esr/faqs/smart-questions.html

CometD provides you with full fledged skeleton applications that you
can create via Maven archetypes, see
http://docs.cometd.org/reference/#primer.
CometD provides you with tutorials, see http://docs.cometd.org/tutorials/.

Try following those, and if you still have a problem, let us know.

For example, I realize now that the documentation is lacking about how
to enable debug logging - which may help you.
I have created this bug about: http://bugs.cometd.org/browse/COMETD-429.

Thanks !

Santh

unread,
May 18, 2013, 4:33:28 PM5/18/13
to cometd...@googlegroups.com
Thanks for a quick & detailed answer. I'll check further & let u know if I get some issues.
Reply all
Reply to author
Forward
0 new messages