Broadcasting to subscribers except message's sender

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Broadcasting to subscribers except message's sender

Oleg
This post was updated on .
My second question is regarding Jersey and broadcasting. I have this simple code (similar to Atmosphere-jQuery sample app)

@Path("/pubsub/{topic}")
@Produces("text/html;charset=ISO-8859-1")
public class WhiteboardPubSub
{
    private
    @PathParam("topic")
    Broadcaster topic;

    @GET
    public SuspendResponse<String> subscribe() {
        return new SuspendResponse.SuspendResponseBuilder<String>().broadcaster(topic).outputComments(true).build();
    }

    @POST
    @Broadcast
    public Broadcastable publish(@FormParam("message") String message) {
        return new Broadcastable(message, "", topic);
    }
}

With this code all published messages will be broadcasted to all subscribers incl. sender of the publish-request. How can I achieve that a message is only broadcasted to subscribers except sender of this message? It doesn't make sense for sender to get the same message again. ;-)

Thanks a lot in advance!
Oleg.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Broadcasting to subscribers except message's sender

Oleg
I've resolved the issue. This is my adjusted class. Current sender is removed from the topic to avoid self-notification.

@Path("/pubsub/{topic}/{sender}")
@Produces("text/html;charset=ISO-8859-1")
public class WhiteboardPubSub
{
    private
    @PathParam("topic")
    Broadcaster topic;

    @GET
    public SuspendResponse<String> subscribe() {
        return new SuspendResponse.SuspendResponseBuilder<String>().broadcaster(topic).outputComments(true).build();
    }

    @POST
    @Broadcast
    public String publish(@FormParam("message") String message, @PathParam("sender") String sender) {
 
        // find current sender in all suspended resources and remove it from the notification
        Collection<AtmosphereResource<?, ?>> ars = topic.getAtmosphereResources();
        if (ars == null) {
            return "";
        }

        Set<AtmosphereResource<?, ?>> arsSubset = new HashSet<AtmosphereResource<?, ?>>();
        for (AtmosphereResource ar : ars) {
            Object req = ar.getRequest();
            if (req instanceof HttpServletRequest) {
                String pathInfo = ((HttpServletRequest) req).getPathInfo();
                String resSender = pathInfo.substring(pathInfo.lastIndexOf('/') + 1);
                if (!sender.equals(resSender)) {
                    arsSubset.add(ar);
                }
            }
        }

        topic.broadcast(message, arsSubset);

        return "";
    }
}
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Broadcasting to subscribers except message's sender

java.net
Hello Oleg,

Thank you for sharing your findings with us

Pierre


On 31-8-2011 17:10, Oleg wrote:

> I've resolved the issue. This is my adjusted class. Current sender is removed
> from the topic to avoid self-notification.
>
> @Path("/pubsub/{topic}/{sender}")
> @Produces("text/html;charset=ISO-8859-1")
> public class WhiteboardPubSub
> {
>      private
>      @PathParam("topic")
>      Broadcaster topic;
>
>      @GET
>      public SuspendResponse<String>  subscribe() {
>          return new
> SuspendResponse.SuspendResponseBuilder<String>().broadcaster(topic).outputComments(true).build();
>      }
>
>      @POST
>      @Broadcast
>      public String publish(@FormParam("message") String message,
> @PathParam("sender") String sender) {
>
>          // find current sender in all suspended resources and remove it from
> the notification
>          Collection&lt;AtmosphereResource&lt;?, ?&gt;>  ars =
> topic.getAtmosphereResources();
>          if (ars == null) {
>              return "";
>          }
>
>          Set&lt;AtmosphereResource&lt;?, ?&gt;>  arsSubset = new
> HashSet&lt;AtmosphereResource&lt;?, ?&gt;>();
>          for (AtmosphereResource ar : ars) {
>              Object req = ar.getRequest();
>              if (req instanceof HttpServletRequest) {
>                  String pathInfo = ((HttpServletRequest) req).getPathInfo();
>                  String resSender =
> pathInfo.substring(pathInfo.lastIndexOf('/') + 1);
>                  if (!sender.equals(resSender)) {
>                      arsSubset.add(ar);
>                  }
>              }
>          }
>
>          topic.broadcast(message, arsSubset);
>
>          return "";
>      }
> }
>
> --
> View this message in context: http://atmosphere-users-mailling-list.2493822.n2.nabble.com/Broadcasting-to-subscribers-except-message-s-sender-tp6745592p6746917.html
> Sent from the Atmosphere users mailling list mailing list archive at Nabble.com.
Loading...