Saturday 15 March 2014

node.js - Long-polling (user notification) with a RESTful architecture -



node.js - Long-polling (user notification) with a RESTful architecture -

i'm working on simple restful api (on nodejs). understand beingness restful implies horizontal scaling much easier. unfortunately, need way clients notified of events instantly. reason, thinking of doing long-polling. question have how work multiple servers. instance, here's simplified used case:

both servers , b behind mutual load balancer. user alice posts message on bob's wall (put request on server a). if bob online, should notified instantly (long-polling on server b).

how can server send notification bob, or server b know bob should notified?

first off, don't know why wouldn't utilize socket.io on both client , server handle notification channel. utilize web sockets if available (more efficient long polling) , fallback long polling if no web sockets.

there number of ways handle issue of notifying user may not connected server originates notification:

connected database

when user connects, load balanced random server, server end connecting stored in central database (e.g. perhaps redis store) of servers can find out server user connected to. gives ability lookup server user connected to. each server handles user connections adds each user database reference server id when connect , removes them database when user disconnects. note: since info doesn't need stored persistently disk , size of info small, can select database excels @ keeping or of info in memory.

hashed connection

based on well-known characteristic of user such userid, hash value computed , repeatable algorithm created map hash values across server pool. predictable assignment. thus, when server wants notify bob, can phone call same function figure out server bob has connected to. hash algorithm can adaptive such if today have 3 servers, hashed users spread evenly among 3 servers , if tomorrow have 4 servers, hashed users spread evenly among 4 servers.

multi-connection

since low-activity websockets pretty scalable these days, users connect servers , each server have socket connection user. simpler infrastructure, not scalable.

node.js rest long-polling

No comments:

Post a Comment