Book Image

Node Web Development - Second Edition

By : David Herron
Book Image

Node Web Development - Second Edition

By: David Herron

Overview of this book

Table of Contents (17 chapters)
Node Web Development Second Edition
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Sending messages between users


Let's try something a little more complex, and closer to the intended purpose of Socket.IO. Let's implement a way for Notes' users to send each other messages within Notes.

When a user logs in, and they have messages, we'll show those messages in a box at the top of the screen. We'll also allow the user to delete those messages, and of course to send messages to themselves or other users.

Let's get started.

Socket.IO events for sending messages between users

The events we'll be sending and receiving are:

  • newmessage: This event indicates that a message has been created. It includes the user ID of the recipient of the message. It's sent from the server, and the code in the browser is to retrieve the current list of messages.

  • delmessage: This event indicates that a message has been deleted. It's sent from the server, and the code in the browser is to retrieve the current list of messages.

  • getmessages: This is sent by the browser-side code, when it's time to retrieve...