Background & Issue
Hello! I am currently coding a chat app in anvil. I am still very new to web apps, so if any of my underlying assumptions that caused me to try and store data in this way are wrong, I’d love some other best practice suggestions. It is my understanding that searching/querying Data Tables can be expensive in time and that storing things in anvil.server.session can be more efficient.
What I’ve Tried
Currently, some aspect of updating the client with new messages is relatively slow and I am trying to optimize this. I am currently attempting to search my Data Table of ‘messages’ with a given criteria and then store the messages in the anvil.server.session. Unfortunately, when I try to set the server session variable or return the server session variable (it is hard to tell which because of how errors of server functions are reported on the client), I get " Exception: Cannot serialize object of type dict". It looks like Python doesn’t allow you to serialize dictionaries – which is what I assume is happening between the client and server.
What is the best practice when passing multiple Data Table rows from the server to the client?
How strange! Because Anvil absolutely can send dicts from client to server, as long as the keys are strings. (Here’s the definitive list of what you can pass between the client and server modules, and it includes both dicts and data table rows!) As @p.colbert has commented, you can return dicts or lists of Data Table rows from server functions just fine – Python’s multiple return values are an idiomatic way to do it.
So: Can you share a code sample that displays this problem? That will help, I think.
One possibility I can think of is that perhaps you’re trying to send the anvil.server.session_state object over in its entirety? That probably won’t work, because the session_state object is special (you’ll need to get specific things out of it and return them), but the error ought to be more informative than “cannot serialise object of type dict”!