Storing multiple rows from a Data Table in anvil.server.session

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.

Ultimate Question
What is the best practice when passing multiple Data Table rows from the server to the client?

Code Sample:

@anvil.server.callable
def get_messages(user, contact):
  anvil.server.session['messages'] = app_tables.messages.search(users=[user], contact_name=contact)
  return anvil.server.session['messages']

returns a SearchIterator, which can be passed directly back to the browser. See Searching (querying) a Table. Try returning it directly.

Also see Views, for a way to pass a pre-filtered table subset to the browser. Your code in the browser can search this as if it was a table. And Accelerated Tables

These are probably better solutions.

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.

Indeed, you can’t transfer a dict directly from server to client, but it can be done easily with the aid of a serialisation module. See:
https://anvil.works/forum/t/serialization-module-anvil-labs-new-feature/12436

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”!