Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Node Cookbook: Second Edition
  • Table Of Contents Toc
Node Cookbook: Second Edition

Node Cookbook: Second Edition

By : David Mark Clements
4.3 (7)
close
close
Node Cookbook: Second Edition

Node Cookbook: Second Edition

4.3 (7)
By: David Mark Clements

Overview of this book

In Node Cookbook Second Edition, each chapter focuses on a different aspect of working with Node. Following a Cookbook structure, the recipes are written in an easy-to-understand language. Readers will find it easier to grasp even the complex recipes which are backed by lots of illustrations, tips, and hints. If you have some knowledge of JavaScript and want to build fast, efficient, scalable client-server solutions, then Node Cookbook Second Edition is for you. Knowledge of Node will be an advantage but is not required. Experienced users of Node will be able to improve their skills.
Table of Contents (18 chapters)
close
close
Node Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1
Index

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.

How to do it...

Let's use require with the ws module and create our WebSocket server (we'll call this wss):

var WSServer = require('ws').Server,
  wss = new WSServer({port:8080});

Now that we have our WebSocket server (wss) instance, we can listen...

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Node Cookbook: Second Edition
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon