Atmosphere: How to unsubscribe from a broadcast

1k views Asked by At

Simple question, how do you unsubscribe from a broadcast from the Java API ? I can't find anything in the API that stands out and I can't find anything on Google.

Stack: Java 7, Atmosphere 2.2.3, Atmosphere-Jersey 2.2.3, Servlet 3, Tomcat 7

This is how I'm subscribing:

@Path("subscription")
public class CurrencyPairSubscription {

    @GET
    @Suspend
    @Path("{pair}")
    public Broadcastable subscribeToQuote(@PathParam("pair") String pair,
            @Context final BroadcasterFactory broadcasterFactory) {

        return new Broadcastable(broadcasterFactory.lookup(pair, true));
    }
}

This is how I'm broadcasting:

public class BroadcasterServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    public void init() {

        System.out.println("Broadcast Servlet init");
        AtmosphereFramework framework = (AtmosphereFramework) getServletContext()
                .getAttribute("AtmosphereServlet");
        MetaBroadcaster metaBroadcaster = framework.metaBroadcaster();


        metaBroadcaster.scheduleTo(RandomQuoteGenerator.generate()
                .getIdentifier(), RandomQuoteGenerator.retrieveLastQuote(), 15,
                TimeUnit.MILLISECONDS);
    }
}
1

There are 1 answers

2
Ken de Guzman On BEST ANSWER

On a client side js/jquery you can call unsubscribe method to close connection.

socket.unsubscribe(request);

While in the server side, you can use AtmosphereResource close method to unsubscribe the user.Note you can try to inject AtmosphereResourceFactory to have an access to all atmosphereResource connection.

@Inject
private AtmosphereResourceFactory resourceFactory;

then find the unique UUID and close the connection

resourceFactory.find(userUUID).close();