Book Image

Node.js By Example

Book Image

Node.js By Example

Overview of this book

Table of Contents (18 chapters)
Node.js By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Getting to know WebSockets


WebSockets is a technology that opens a two-way (bidirectional) interactive channel between the server and the browser. By using this type of communication, we are able to exchange messages without the need of an initial request. Both sides simply dispatch events to each other. The other benefits of WebSockets are lower bandwidth requirement and latency.

There are a couple of ways to transfer data from the server to the client and vice versa. Let's check the most popular ones and see why WebSockets is considered the best option for real-time web apps:

  • Classic HTTP communication: The client requests a resource from the server. The server figures out what the response should be and sends it. In the context of real-time applications, this is not very practical because we have to manually ask for more data.

  • Ajax polling: It is similar to the classical HTTP request except for the fact that we have the code that constantly sends requests to the server, for instance, in...