O'Reilly logo

Node Cookbook Second Edition by David Mark Clements

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a WebSocket server

For this task, we will use the third-party ws module to create a pure WebSocket server that will receive and respond to WebSocket requests from the browser.

Getting ready

We'll create a new folder for our project that will hold two files: server.js and client.html. The client.html file will provide a basic user interface and connect to the WebSocket server while server.js supplies the server-side WebSocket functionality and serves up the client.html file in response to browser requests. We also need to install the ws module. Once we've changed the directory in our new folder on the command line, we can run the following code:

npm install ws

Note

For more information on the ws module, refer to https://www.github.com/einaros/ws ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required