5

How do I broadcast messages from only one client to another with Atmosphere (Meteor)?I have currently this implementation based on meteor tutorial

@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
    Meteor.build(req).addListener(new AtmosphereResourceEventListenerAdapter());
}

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
    String body = req.getReader().readLine().trim();
    //some DAO lookups - here I would like to say I want to broadcast only to concrete client
    BroadcasterFactory.getDefault().lookup(DefaultBroadcaster.class, "/*").broadcast(UserDAO.getInstance().getUser(name));
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115

3 Answers3

3

Here is the Atmosphere FAQ on their wiki: https://github.com/Atmosphere/atmosphere/wiki/Creating-private-channel-of-communication-between-Browsers

halfer
  • 19,824
  • 17
  • 99
  • 186
jfarcand
  • 1,705
  • 9
  • 6
3

Another solution I believe : for adressing only one client, you don't need to broadcast, you may just do this:

     try
     {
        r.getResponse().write(message);
     }
     catch(IllegalStateException e)
     {
        logger.error("Could not send message through atmosphere " + userId);
     }

where r is the resource that you can memorize in your program.

unludo
  • 4,912
  • 7
  • 47
  • 71
  • I am saving the `AtmosphereResource` s in a static Map in another class. Then iterate over them and call the `write` method you shown, but the client recieves nothing. – kiltek Aug 08 '17 at 10:03
  • @kiltek it has been a while but it's likely that this solution works a bit like an http session. So you can't keep it for another time, just use it in the "session" – unludo Aug 08 '17 at 14:23
0
BroadcasterFactory.getDefault().lookup(atmosphereResource.uuid()).broadcast('something');
Xiujun Ma
  • 2,574
  • 1
  • 14
  • 19