1

How can i get latest or fresh data from server (if in server happened new event (for example there are 2 users x,y and x send messages to y and y get this message without refreshing page ) )? I don't want to use setInterval because it repeats all message again and again. Is there any Technique that can i use for this ? I heard about Ajax that technique need to send request to the server but i want when happen an event in the server and webpage get it without refreshing..

2 Answers2

1

The first technique is the long polling, which sends request to the server and waits until the server sends something, for example the new message. You must re-send requests to the server each time you get a new message or your request is time out. This technique uses AJAX. Long polling PHP example - How do I implement basic "Long Polling"?

The second is web sockets, https://en.wikipedia.org/wiki/WebSocket

this stackoverflow question deals with the implementation of websocket.

socket.io has a demo of chat application.

Community
  • 1
  • 1
Hovo
  • 790
  • 4
  • 21
  • Can i have some more details on websockets how its works and maybe a small exemple ? i saw a tut but i didnt understand that means **var socket =new Websocket("ws://echo.websocket.org");** –  May 30 '15 at 15:51
  • What backend platform do you use? – Hovo May 30 '15 at 15:57
1

If you looking for bidirectional full duplex method then go for WebSockets but for just polling data from server you can use Server Sent Event as well. Adding reference links for both:

WebSocket:

http://html5demos.com/web-socket

http://en.wikipedia.org/wiki/WebSocket

HTML5 Websockets for Realtime Chat app?

SSE:

http://www.html5rocks.com/en/tutorials/eventsource/basics/

Examples:

SSE: http://demo.howopensource.com/sse/

Community
  • 1
  • 1
Avinash
  • 6,064
  • 15
  • 62
  • 95
  • what is difference between sse and websocket ? –  May 30 '15 at 15:31
  • @Aryan if you want to send and receive data from server then you need to use `WebSocket` (Chatting System) but if you want to just fetch data from server then go with `SSE` (Stock Exchange Terminal) – Avinash May 30 '15 at 15:36
  • Can i have some more details on websockets how its works and a small exemple ? –  May 30 '15 at 15:41
  • @Aryan added link for SSE example, I will add for WebSocket once I will find a good one. – Avinash May 30 '15 at 15:53