mayank
mayank

Reputation: 1

Server push using COMETD to client(dojo)

I am trying to push message from server to client. I am using DOJO 1.7, Cometd and Jetty integrated with tomcat6.

 //Server side code
   public class notificationService extends AbstractService {

public notificationService(BayeuxServer bayeux, String name) {
    super(bayeux, name);
    System.out.println("Inside constrcutor of Notification Service");
    addService("/notification", "processNotification");
}


    public void processNotification(ServerSession remote,ServerMessage.Mutable message)      
        {
    System.out.println("Inside process Notification");
    Map<String,Object> response = new HashMap<String,Object>();
    response.put("payload",new java.util.Date());
            getBayeux().createIfAbsent("/notification");
          getBayeux().getChannel("/notification").publish(getServerSession(),response,null);
            //remote.deliver(getServerSession(),"/notification", response, null);
        }

      //Client Side Code (DOJO)

       var cometd = dojox.cometd;
       cometd.init("http://serverip:port/cometd")
       cometd.publish('/notification',{ mydata: { foo: 'bar' } });
       cometd.subscribe('/notification', function(message)
            {
                    //alert("Message received" + message.data.payload);
                    //alert(message.data.payload);
                    alert("Message received");
            });

I want to broadcast message to all the clients subscribe to a particular channel. When m using remore.deliver it is sending message to individual client but not to all clients subscribed to that channel. channel.publish is not working for me...any help and comments are highly appreciated.

Upvotes: 0

Views: 1155

Answers (1)

sbordet
sbordet

Reputation: 18637

Your client is publishing to channel /notification, which is a broadcast channel (see http://docs.cometd.org/reference/#concepts_channels for the definition) so that message will not only be received by your service, but also broadcast to all subscribers of that channel.

Then in your service you call ServerChannel.publish() that will send another message to the subscribers of the /notification channel (apart the service itself - there is an infinite loop prevention).

A better way to perform this kind of actions is to use a service channel, for example /service/notification, to send the initial message from your client to the service. Then the service can broadcast, like it does now, to channel /notification.

Calling ServerChannel.publish() is the right way to broadcast messages from a service. Unfortunately you do not specify exactly why is not working for you, so I cannot help.

I would start by using a service channel between your client and the service for the first message.

Note also that Tomcat 6 is not really the best solution for CometD, since it does not support asynchronous servlets (Servlet 3).

You are better off using a Servlet 3 compliant container such as Jetty 8.

Upvotes: 2

Related Questions