Skip to content

Is it possible to sync state across clients or broadcast from the server? #966

Discussion options

You must be logged in to vote

@niklas-heer I found Ryan's example above too complicated to digest, so I wrote a minimal version for you.

import asyncio
import random

from idom import component, hooks, html, run


async def get_votes_task(set_votes):
    """Periodically performs a database query then runs `set_votes(value)`
    to let the component know about the results."""
    while True:
        value = random.randint(0, 10000)  # Replace this with database logic
        set_votes(value)
        await asyncio.sleep(5)


@component
def example():
    votes, set_votes = hooks.use_state(None)  # Votes will be stored in this

    @hooks.use_effect(dependencies=[])  # No dependencies, so this will run only once (to star…

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@rmorshea
Comment options

@Archmonger
Comment options

@rmorshea
Comment options

@Archmonger
Comment options

Answer selected by Archmonger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants