broadcast?

3 views
Skip to first unread message

Federico Fissore

unread,
Nov 23, 2009, 12:10:26 PM11/23/09
to cometd-users
Hi all

I'm playing with cometd, trying to implement an Hello World

I've started from the "primer" and extended it with an EchoService
public EchoService(Bayeux bayeux) {
super(bayeux, EchoService.class.getName());

subscribe("/my/echo", "echo");
}

public void echo(Client client, String channelName, Object data,
String messageId) {
for (Client c : getBayeux().getChannel(channelName,
false).getSubscribers()) {
send(c, channelName, "Hello " + data + "!", messageId);
}
}

while on the (dojo) client side, the clients subscribe to the channel
"/my/echo" and publish events on it as the user click a "send" button

what happens is that the clients are receiving TWO messages. One with
the "hello", the other with just what the user has typed. I've
expected to see just the "Hello" version: why is it repeating?

I know I'm missing something but can't get it so far. Any hint?

Simone Bordet

unread,
Nov 23, 2009, 12:25:42 PM11/23/09
to cometd...@googlegroups.com
Hi,
The behavior of the server, for a non-service channel, is that, when
it receives a message, it sends it to all clients that subscribed to
that channel (otherwise the server would do nothing at all).
The server received a message on channel /my/echo and sees that it has
2 clients subscribed: a remote client (you subscribed via the JS API),
and a server-side client you subscribed in EchoService's constructor.

On server-side, delivering that message results in your "echo" method
to be called, where you send again a message to all clients subscribed
to the /my/echo channel.

To solve the issue you have 2 options:

1. The JS client sends a message to a service channel.
2. You don't subscribe your JS client to the /my/echo channel.

You can find references to normal channels vs service channels here:
http://cometd.org/documentation/cometd-javascript/subscription

Simon
--
http://bordet.blogspot.com
---
Finally, no matter how good the architecture and design are,
to deliver bug-free software with optimal performance and reliability,
the implementation technique must be flawless. Victoria Livschitz

Simone Bordet

unread,
Nov 23, 2009, 12:44:00 PM11/23/09
to cometd...@googlegroups.com
Hi,

On Mon, Nov 23, 2009 at 18:10, Federico Fissore
<federico...@gmail.com> wrote:
> Hi all
>
> I'm playing with cometd, trying to implement an Hello World
>
> I've started from the "primer" and extended it with an EchoService
>  public EchoService(Bayeux bayeux) {
>    super(bayeux, EchoService.class.getName());
>
>    subscribe("/my/echo", "echo");
>  }
>
>  public void echo(Client client, String channelName, Object data,
> String messageId) {
>    for (Client c : getBayeux().getChannel(channelName,
> false).getSubscribers()) {

Unless you want to skip some client, you can do this:

getBayeux().getChannel(channelName, false).publish(getClient(), data, null);

See also http://cometd.org/documentation/cometd-java/server/channel.

I would appreciate any comment you can make on how difficult is to
setup a dev environment, as I am writing documentation on that.
Your comments will add to the ones that other already provided, which
turn out to be very useful in writing the documentation.
See this thread:
http://groups.google.com/group/cometd-users/browse_thread/thread/286bd41d9b2e0228/

Thanks,
Reply all
Reply to author
Forward
0 new messages