March 2018
Beginner to intermediate
344 pages
7h 7m
English
In order to send a new message, we can listen for an event named chatMessage within our server.js file.
This can be done inside of our original connection event, ensuring that we listen for events on a socket-by-socket basis:
io.on('connection', socket => { console.log('A user connected.'); socket.on('chatMessage', message => { console.log(message); })});
If we then send the chatMessage event from our client, it should subsequently log out this message inside of our Terminal. Let's try it out!
Because we've made a change to our server.js file, we'll need to restart the Node instance. Hit CTRL + C within the Terminal window that is running server.js and run node server.js again.
Read now
Unlock full access