Hi! I am just starting with Anvil and I think I have gotten to the point where I am out of my depth. I am coding a chat app and I have been a little bit stumped on the best practice for refreshing a signed in user (done with google integration) is sent a message by another user.
Messages are stored in a table and then (currently) only retrieved from the table or server environment when a user sends a message. The client side of the app has a refresh() function which is called to update a repeating panel with the new messages. So I am actually getting the messages from other clients (yay!). I am not sure how to let a client know when to refresh because somebody else sent them a message.
I think the outstanding questions TLDR are:
- How do I identify clients from the server side? (i.e. Which user in the users table is logged into any given client) Or do I do this only from the client side?
- How do I tell them to refresh?
I could break out some code, but I am not exactly sure what would be most relevant to share at the moment.
Thanks in advance for the help!
This is typically done in a Timer component’s tick handler event, calling a server function to get recent activity, if you want it to appear to be somewhat asynchronous.
Your Messages table should have a column for identifying which user posted a message. A lot of people will do the simple thing and link to a row in the Users table, but that exposes the entire Users row to all the clients (including their hashed passwords).
From a security perspective, it’s probably better to store a user name or something like that in the Messages table so that a user doesn’t see other users’ User table rows. But for a learning app, do the simple thing.
Hi @jshaffstall! Thanks for the quick, helpful response! Just to clarify, the tick handler would be running on each client, asking the server for new activity. The server will feed it back new activity which was sent to username provided when the client called the function. Afterwards, would I out the server’s recent activity pool? Of just the recent messages sent to that specific user?
Thanks again, that explanation really helped.
That depends entirely on how you want the app to work.