Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to publish a message to a Redis channel named 'alerts'.
Redis
redis_client.publish('alerts', [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the channel name as the message instead of a string message.
Forgetting to put quotes around the message.
✗ Incorrect
The publish method requires the channel name and the message. The message here is a string 'New alert!'.
2fill in blank
mediumComplete the code to subscribe to the Redis channel 'alerts' using a Redis client.
Redis
redis_client.subscribe([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the channel name without quotes.
Using the wrong channel name.
✗ Incorrect
To listen for messages, you subscribe to the channel named 'alerts' as a string.
3fill in blank
hardFix the error in the code to correctly handle incoming messages from the 'alerts' channel.
Redis
redis_client.on('message', function(channel, [1]) { console.log('Received:', [1]) });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that does not match inside the function.
Confusing the channel name with the message parameter.
✗ Incorrect
The callback function receives the channel and the message as parameters. 'msg' is a common name for the message parameter.
4fill in blank
hardFill both blanks to publish a JSON string notification with a type and content to the 'notifications' channel.
Redis
const notification = JSON.stringify({ type: [1], content: [2] });
redis_client.publish('notifications', notification); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around string values.
Mixing up type and content values.
✗ Incorrect
The notification object needs a type and content as strings. 'info' and 'Hello, user!' are valid values.
5fill in blank
hardFill all three blanks to subscribe to the 'notifications' channel and log parsed JSON messages with type and content.
Redis
redis_client.subscribe([1]); redis_client.on('message', (channel, [2]) => { const data = JSON.parse([3]); console.log(`Type: ${data.type}, Content: ${data.content}`); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong channel name in subscribe.
Parsing the wrong variable inside the callback.
✗ Incorrect
Subscribe to 'notifications' channel, use 'msg' as the message parameter, and parse 'msg' to get the JSON data.