I’m glad you found my example useful. You can convert it into a one-to-one chat system by replacing the to_channel column in the messages table with a to_user column that links to the users table. Then each message is from a user to another user.
The UI is simple to modify too: Change the ‘Channel’ dropdown to select a user instead. When a user is selected in the dropdown, display the messages from that user to the logged-in user, and from the logged-in user to the selected user.
Then instead of channels, you will have a one-to-one chat history between each pair of users.
Table Messages has these entities:Column SentTime, Column Read, Column FromUser and Column ToUser. Then table Replies has these entities: Link to users, Text and and Link to table messages. I am just going to alternate them using Conditional programming.
Hi @shaun I love your commenting system for it also helped me with the App that I am creating on other fields. I will also recognize you in the app that I am creating for you made other things easy for me.
I’ve updated the CSS to show the chat messages in bottom up manner, instead of top down.
.anvil-role-conversation-panel {
display: flex;
flex-direction: column-reverse;
border: 1px solid #888;
overflow-y: scroll;
overflow-x: hidden; /* ew, but I'm not debugging CSS for the next hour */
height: 60vh;
}
This modified CSS also eliminated the requirement of “scrollDown()” function. Thought it might be useful for some people.
Even though, this is exactly what I wanted for my experimental chat app; however, I’m still not quite sure how/why the items are getting populated in non-reverse order. Shouldn’t the CSS “flex-direction: column-reverse” affect the ordering?
In either case, you don’t need CSS to change the order from oldest-first to newest-first. You can simply reverse the order of the messages list in the Python code.
Reversing the list order is a bit trickier in the Magical Concierge app because it’s only fetching the messages it doesn’t know about, rather than refreshing the entire message list each time. I’ll leave that one as an excercise for the reader ( ), but here’s a hint: you can import the Data Tables query operators:
import anvil.tables.query as q
which will help with finding which messages have been created since last_seen_event.