Search code examples
javajavascriptcometdajax-pollingbayeux

How can I publish a message to all Javascript subscribers for a channel using CometD?


Given a Java web application using CometD, how can I publish a message to all of a channel's subscribers each time a message is processed by bayeux?

For testing, I used serverSession.deliver(serverSession, "/test-channel", map, null);.

The result is that my Javascript client will subscribe successfully, but only receive messages 1 out of 10 tries. So, 90% of the time, no messages are ever received by the browser, yet the Bayeux logging (level 3) shows that they are being sent to the correct channel every time.

Should I be using some form of a client session? The CometD documentation appears to be a bit unclear.


Solution

  • Using the localSession seems to be the answer. I implemented this to fix:

    ClientSessionChannel channel = localSession.getChannel(getClientChannel());
    channel.publish(map);
    

    The clients are now receiving all messages.