Question

I am trying to implement a scenario when I need to notify a specific client say A when another client B makes a GET request to the server. I am looking at Atmosphere for server push. I have successfully connected B to the server and able to Push him if sending messages through Atmosphere. But this particular scenario I am stuck. Can any one please present a viable solution? Else some pointers in the direction?

import org.atmosphere.annotation.Broadcast;
import org.atmosphere.annotation.Suspend;
import org.atmosphere.config.service.AtmosphereService;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter;
import org.atmosphere.jersey.JerseyBroadcaster;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

/**
 * Simple chat resource demonstrating the power of Atmosphere. This resource supports transport like WebSocket, Streaming, JSONP and Long-Polling.
 *
 * @author Jeanfrancois Arcand
 */
@Path("/")
@AtmosphereService (broadcaster = JerseyBroadcaster.class)
public class ChatResource {

    /**
     * Suspend the response without writing anything back to the client.
     *
     * @return a white space
     */
    @Suspend(contentType = "application/json", listeners = {OnDisconnect.class})
    @GET
    public String suspend() {

        return "";
    }

    /**
     * Broadcast the received message object to all suspended response. Do not write back the message to the calling connection.
     *
     * @param message a {@link Message}
     * @return a {@link Response}
     */
    @Broadcast(writeEntity = false)
    @POST
    @Produces("application/json")
    public Response broadcast(Message message) {
        return new Response(message.author, message.message);
    }

    public static final class OnDisconnect extends AtmosphereResourceEventListenerAdapter {
        /**
         * {@inheritDoc}
         */
        @Override
        public void onDisconnect(AtmosphereResourceEvent event) {
            System.out.println(event);
        }
    }

}
Was it helpful?

Solution

Instead of using @Broadcast, use Broadcaster directly using the BroadcasterFactory, and invoke broadcaster.broadcast(message, ClientB). Look at atmosphere-sample's, there is many example doing that.

-- Jeanfrancois

OTHER TIPS

Use broadcaster directly by using BroadcasterFactory and invoke broadcaster.broadcast(message,ClientB)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top