architecture - Using Pub/Sub and Long Polling -


i want implement scalable chat application. therefore, use pub/sub technique (when sends message, been sent whole room). because of simplicty, chose redis cache server.

in addition, use long polling transfer new messages server client. reason not using web sockets because can have troubles in organization's proxies.

as far read on web, sounds standard solution problem.

can please advice best solution handle messages been sent between long poll requests? how user doesn't miss message?

it sounds need save cache each client, containing own messages. way - not leverage pub/sub technique. instead of pub/sub, can send message each user in room.

using long polling approach, client might make api calls server (in example see chats in room 1034, has 3 chats in it):

get /rooms/1034/chats 

since there data had, server responds straight away.

response:

{   "chats": [     { "sequence": 0, "by": "fred", "message": "hey guys, in room?" },     { "sequence": 1, "by": "fred", "message": "anyone @ all?" },     { "sequence": 2, "by": "bill", "message": "yeah i'm here!" }   ] } 

now client can display 3 chats, , ask server more. long polling kicks in - since server has no more chats, api call blocks until new chat arrrives.

get /rooms/1034/chats?after=2 

.. server blocks until adds new chat ..

response:

{   "chats": [     { "sequence": 4, "by": "frodo", "message": "i'm here too!" }   ] } 

the key point client maintains "cursor" of is, don't need maintain per-client cache, etc. on server.

of course there's more it. server cannot block forever, after time (maybe 60 seconds) should reply, timeout message.

in spring there support via deferredresult server handle without tying whole thread per client.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -