Is it possible to sync state across clients or broadcast from the server? #966
-
First, I'm new to ReactPy, so I'm sorry if my question has already been answered or is obvious, but I couldn't find anything on it on the website or here in the discussion. I want to build a simple voting app where multiple people can connect at the same time and send their vote. For that, I would need to sync state across multiple clients or broadcast from the server. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hey @niklas-heer, all state is maintained server-side so this is definitely possible. So, while this is likely not realistic in a production environment, if you only run the server on a single process you could share state between clients using a global variable: poor_mans_database = {
"votes": 0
}
@component
def example():
def increment_votes():
poor_mans_database["votes"] += 1
return html.div(
html.button(
{"on_click": lambda e: increment_votes()}, "click to vote"
),
html.p("current vote total: ", poor_mans_database["votes"])
) i'm on mobile now, can get back with more detail if needed |
Beta Was this translation helpful? Give feedback.
@niklas-heer I found Ryan's example above too complicated to digest, so I wrote a minimal version for you.