Skip to content

Virtuoso Message List Tutorial - Receive Messages

In this part, we’re going to implement a simulation of receiving messages from the server. Since we have no actual backend, we’re going to use a button for that. Receiving messages has a specific scroll behavior; if the list is at the bottom, it should automatically scroll to the bottom. If the user has scrolled up to read previous messages, the list should remain at the same scroll position and display a notification that new messages are available. Let’s add a counter for the unread messages first:

ts

Then, add the following button somewhere below the message list:

tsx

If everything works as expected, pressing the button will display a new message from another user with a smooth scroll animation.

We will use the add the new message counter next to the scroll to bottom button, by passing the unseenMessages counter through the context prop. Let’s extend our context and pass the additional flag:

tsx

Now, change the StickyFooter component to display the new message counter:

tsx

A more sophisticated logic can be implemented, but for now, we would like to reset the counter when the user scrolls to the bottom. Add the following code to the onScroll event handler:

tsx

Scroll up the message list and press the button - the indicator of new messages should appear. Clicking the button will scroll to the bottom of the list and reset the indicator.