HTML and CSS Reference
In-Depth Information
event ID and a retry interval. These pieces of information can be accessed as the id and retry properties of
the event parameter.
Although you don't need to do so in this example, you can stop receiving server-sent events by calling
the EventSource object's close() method.
n Note In the example discussed in this section, server-side processing takes place for 1 minute, after which the
server terminates the underlying connection. The browser, however, thinks there was a connection problem and after
a brief interval tries to reconnect with the server, causing the same logic to run again. If you don't wish to receive
further events, you need to call the EventSource object's close() method.
Two-Way Communication Using Web Sockets
Typically, communication over the Web consists of two distinct parties participating: the client and the
web server. So far in this chapter you've learned about two client-server communication techniques: one-
way communication and request-response communication.
Server-sent events use the one-way communication model. In one-way communication, one party
communicates with the other party. In the case of server-sent events, the server keeps “talking” with the
client by sending notifications. This is also referred to as simplex communication. A real-life example of
simplex communication is radio broadcasting, where radio signals are sent from the radio station but the
station doesn't receive anything back.
The postMessage API and XMLHttpRequest object use the request-response model to communicate
with the server. In this model, a client initiates a request with the server to trigger some processing or fetch
some data. The server then sends the response back to the client once the processing is finished. The
underlying communication channel is held open during only one request-response cycle. If you send
multiple requests to the server, you're essentially opening and closing the communication channel that
many times. Such a communication pattern is sometimes called half-duplex communication because at
any one time, either the client or the server is talking to the other party. Of course, in a web application, the
browser has to initiate communication with the server; only then is the server allowed to respond. A real-
life example of half-duplex communication is a walkie-talkie: only one person can talk at a time.
There is a third type of communication—two-way or duplex communication. In this case, both parties
can communicate at the same time. A real-life example is a telephone call: both the parties can talk
simultaneously. A common application of duplex communication in software applications is chat systems
such as MSN, Yahoo! Messenger, and Google Talk. In any chat system, two or more members can chat with
each other at the same time. Another example is multiplayer online games, where multiple players can
participate at once. As far as HTML5 is concerned, the technique to achieve two-way communication is
Web Sockets.
Understanding Web Sockets
Unlike the request-response model, Web Sockets keep the underlying communication channel open
throughout the course of communication. Web Socket-based communication typically involves three
Establish a connection between the client and the server or handshake.
Ask the Web Socket server to listen to the incoming communication.
Send and receive data.
Search WWH ::

Custom Search