Implementing chat services
I believe that it's more or less clear how WebSockets works now, and we can apply the API for our chat. However, in a real application, we need something more than to echo sent texts. Let's put the intended event scenarios on paper:
- The
Welcome
component handles user input and sends via the client to thejoin
server event with the entered user name in the payload - The server receives the
join
event, adds a new user to the set, and broadcasts theparticipants
event with the updated set - The client receives the
participants
event and passes the set to theParticipants
component, which updates the participant's list - The
Conversation
component handles user input and sends the entered message via the client to the server as thetext
event with username, text, and timestamp in the payload - The server receives the
text
event and broadcasts it to all the chat participants
As we deal with event messages, we need a unified format for sending and receiving a single source of truth...