0
0
Expressframework~10 mins

Emitting and receiving messages in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to emit a message named 'greet' with the text 'Hello'.

Express
socket.[1]('greet', 'Hello');
Drag options to blanks, or click blank then click option'
Aemit
Blisten
Con
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'emit' will not send a named event.
Using 'on' or 'listen' is for receiving messages, not sending.
2fill in blank
medium

Complete the code to listen for a message named 'chat' and log its data.

Express
socket.[1]('chat', (data) => {
  console.log(data);
});
Drag options to blanks, or click blank then click option'
Areceive
Bon
Csend
Demit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'emit' tries to send a message instead of listening.
Using 'send' or 'receive' are not valid socket methods.
3fill in blank
hard

Fix the error in the code to correctly emit a message named 'update' with an object.

Express
socket.[1]('update', { status: 'ok' });
Drag options to blanks, or click blank then click option'
Alisten
Bsend
Con
Demit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'emit' will not send the named 'update' event.
Using 'on' or 'listen' is for receiving, not sending.
4fill in blank
hard

Fill both blanks to listen for a 'message' event and respond by emitting 'reply' with 'Received'.

Express
socket.[1]('message', (msg) => {
  socket.[2]('reply', 'Received');
});
Drag options to blanks, or click blank then click option'
Aon
Bemit
Csend
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'emit' to send the reply.
Using 'emit' to listen or 'on' to send causes errors.
5fill in blank
hard

Fill all three blanks to emit 'start', listen for 'progress', and emit 'done' when progress reaches 100.

Express
socket.[1]('start');
socket.[2]('progress', (percent) => {
  if (percent === 100) {
    socket.[3]('done');
  }
});
Drag options to blanks, or click blank then click option'
Aemit
Bon
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'emit' to send events.
Using 'on' to send events or 'emit' to listen causes errors.