Now that you know how to use WebSockets on the client, it’s time to learn how to use WebSockets on the server. To enable support for WebSockets on the server, you’ll use the ws
npm package, a WebSocket server implementation for use with Node.js applications.
The ws
package provides both a server and a client implementation. The client implementation is only intended for use on Node.js, to enable server to server WebSocket connections. When reading the documentation for the ws
package, focus on the following server examples:
After reading the documentation for the ws
package, you should be able to:
ws
package to create a standalone WebSocket server;ws
package to create a WebSocket server that shares a Node.js http
server with an Express application;connection
event handler listener method to detect when a client has connected to the WebSocket server;close
event handler listener method to detect when a client has closed the connection to the WebSocket server; andsend()
method to send a message to a client.