Broadcasting to a subset of subscribers in Atmosph

2020-07-24 04:57发布

What I'm trying to do: Be able to have users subscribed to a number of different 'chat rooms' and use reverse AJAX / comet to send messages from a chat room to everyone logged into that room. (a bit more complicated but this is a similar use case).

What I'm doing: Using Grails with JMS and Atmosphere. When a message is sent, I'm using JMS to send the message object which is received by a Grails service which is then broadcasted to the atmosphere URL (i.e. atmosphere/messages).

Obviously JMS is a bit redundant there but I though I could use it to help me filter who should retrieve the message although that doesn't really look it'll work (given that the subscriber is basically a singleton service...).

Anyway, what I need to be able to do is only send out a message to the correct subset of people listening to atmosphere/messages. A RESTful-type URL will be perfect here (i.e. atmosphere/messages/* where * is the room ID) however I have no idea how to do that with Atmosphere.

Any ideas / suggestions on how I can achieve what I want? Nothing is concrete at all here so feel free to suggest almost anything. I've even been thinking (based on the response to another question), for example, if I could do something like send out messages to a Node.js server and have that handle the reverse AJAX / comet part.

3条回答
趁早两清
2楼-- · 2020-07-24 05:27

I would recommend you create an AtmosphereHandler for one URL like /atmosphere/chat-room and then use the AtmosphereResource and bind an BroadcastFilter with it, lets say name it ChatRoomBroadcastFilter.

Whenever a user subscribes to a new chat room, a message would be sent to the server (from the client) telling the server about the subscription. Once subscribed, maintain the list of users <> chat room bindings somewhere on the server.

Whenever a message is broadcasted, broadcast it with the chat room id with it. The in the ChatRoomBroadcastFilter (You probably need to make this a PerRequestBroacastFilter) propagate the message to the user only if the user subscribed to the chat room. I am not sure if this clears it out. If you need code example please mention in the comments. I'll put that but that needs some time so ain't putting it right now ;).

查看更多
放我归山
3楼-- · 2020-07-24 05:47

There is a misunderstaning of the concept of comet. Its just another publish/subscribe implementation. If you have multiple chat-rooms, then you need to have multiple "topics", i.e. multiple channels the user can register to. E.g.:

broadcaster['/atmosphere/chatRoom1'].broadcast('Hello world!')

broadcaster['/atmosphere/chatRoom2'].broadcast('Hello world!')

So I would advance you to creaet multiple channels and do not filter manually the set of users, which should retrieve messages (which is definitely not the way it should be done). You do not need to create anything on the server side on this, since the user will just register for a specific channel and receive messages, which anyone is putting into it.

查看更多
霸刀☆藐视天下
4楼-- · 2020-07-24 05:53

If I understand your requirements correctly the following should work (jax-rs + scala code):

1) Everyone who wants to get messages from a chat room registers for it:

@GET
@Path(choose/a/path)
def register(@QueryParam("chatroomId") chatroomId: Broadcaster) {
  // alternatively, the Suspend annotation can be used
  new SuspendResponse.SuspendResponseBuilder[String]()
          .resumeOnBroadcast(false).broadcaster(chatroomId).scope(SCOPE.REQUEST)
          .period(suspendTimeout, TimeUnit.MINUTES)
          .addListener(new AtmosphereEventsLogger()).build
}

2) To broadcast a message for all the registered users, call the following method:

@POST
@Broadcast
@Path(choose/a/path/{chatroomId})
def broadcast(@PathParam("chatroomId") id: String) {
  // first find your broadcaster with the BroadcasterFactory
  BroadcasterFactory.getDefault().lookupAll() // or maybe there is a find by id?
  broadcaster = ...
  broadcaster.broadcast(<your message>)
}

I also recommend reading the atmosphere whitepaper, have a look at the mailing list and at Jeanfrancois Arcand's blog.

Hope that helps.

查看更多
登录 后发表回答