asp.net mvc - How does show the updates asynchronously?
on stack overflow site, can see following updates asynchronously.
- reputation changes
- new question added in question list
- new comment added, etc.
as per understanding, can done in xmlhttprequest (xhr) request asynchronously, maybe of setinterval
.
confusions: in firefox, no xhr request coming , can see above changes asynchronously.
which kind of implementation , how can done in asp.net mvc?
this impressive , beautiful stuff asynchronous calls made client server using ajax.
a simple approach use jquery ajax make asynchronous call inside setinterval search "updates" of question (this question id 17779628. can see on url =p). so, call server can pass id , timestamp (a date) of last call made server. server brings updates occurred timestamp datetime.now
.
i not sure real implementation of stack overflow, did lot of stuff this.
another tip:
there improvement. ;) modern browsers contains implementations of websocket. since sockets "peer-to-peer" , not "client-server", modern browsers don't need approach setinterval. instead, can implement websocket opening in javascript , server can send updates actively in moment happens (you can use design patterns include events).
take @ caniuse see browsers supports websocket.
edit
anyway, opened code you. stack overflow's javascript code uses singleton design pattern javascript code. take @ stackexchange
variable in browser console. heart of javascript code here. can see, there whole api built upon stackexchange
variable. now, search stackexchange.realtime.init('sockets.ny.stackexchange.com:80');
then, take @ implementation of stackexchange.realtime.init
@ console. seems support asynchronous updates websocket. nice , modern, supported new browser versions. if need maintain backward compatibility, can support both websocket (for new) , ajax implementation (for old browsers).
Comments
Post a Comment