Skip to Content
Advanced Node.js Development
book

Advanced Node.js Development

by Andrew Mead
March 2018
Intermediate to advanced
592 pages
13h 44m
English
Packt Publishing
Content preview from Advanced Node.js Development

Wiring up the createMessage listener for all users

To get started, we'll update the createMessage listener. Currently, all we do is log the data to the screen. But here, instead of just logging it, we actually want to emit a new event, a newMessage event, to everybody, so each connected user gets the message that was sent from a specific user. In order to get that done, we'll call a method on io, which will be io.emit:

socket.on('createMessage', (message) => {  console.log('createMessage', message);  io.emit});

Socket.emit emits an event to a single connection, whereas io.emit emits an event to every single connection. Here, we are going to emit the newMessage event, specifying it as our first argument. The second argument, as with socket.emit ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Node.js Development

Learning Node.js Development

Andrew Mead
Serverless Applications with Node.js

Serverless Applications with Node.js

Aleksandar Simovic, Slobodan Stojanovic
Node.js: Tools & Skills, 2nd Edition

Node.js: Tools & Skills, 2nd Edition

James Hibbard, Michael Wanyoike, Nilson Jacques, Jay Raj, Manjunath M
Your First Week With Node.js, 2nd Edition

Your First Week With Node.js, 2nd Edition

James Hibbard, Craig Buckler, Mark Brown, Nilson Jacques, James Kolce, Paul Orac, M. David Green, Florian Rappl

Publisher Resources

ISBN: 9781788393935Supplemental Content