From the course: WebSocket Communications with Node and Angular

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Manage multiple connected users

Manage multiple connected users

- [Instructor] To manage multiple users chatting, we need to refactor our code to better separate concerns. Our connection handler is both managing connections and managing messages, so it's getting crowded. Let's add a new user manager class, dedicated to keeping track of the connected users and offering helper methods to send them messages. Within the file browser, we want to be in packages, server, src, app. And next to the ws-handler that we created earlier, we'll have a user-manager. UserManager will maintain a set of sockets and helper methods to manage them. So we need a method to add the socket, to remove the socket for when a user disconnects, to send a socket a message. We haven't yet defined what a message will look like, so I'll just put unknown for now. Now, we cannot just send a raw object, we have to stringify it first. So I'm going to use JSON.stringify on the message and then send the result. And, of…

Contents