0
0
Redisquery~10 mins

Real-time notification pattern in Redis - Interactive Code Practice

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

Complete 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'
A'New alert!'
Balerts
Cpublish
D'alerts'
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.
2fill in blank
medium

Complete 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'
Aalerts
B'alerts'
C'messages'
D'subscribe'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the channel name without quotes.
Using the wrong channel name.
3fill in blank
hard

Fix 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'
Amsg
Bmessage
Cdata
Dalert
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.
4fill in blank
hard

Fill 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'
A'info'
B'Hello, user!'
C'alert'
D'Welcome message'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around string values.
Mixing up type and content values.
5fill in blank
hard

Fill 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'
A'notifications'
Bmsg
Cmessage
D'alerts'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong channel name in subscribe.
Parsing the wrong variable inside the callback.