Skip to content

How to implement server push ? #1970

Answered by masenf
PasqualePuzio asked this question in Q&A
Discussion options

You must be logged in to vote

A reflex app already maintains a bidirectional websocket connection between the frontend and backend. The best mechanism to notify the client depends on the nature of the "push". The simplest way to achieve this is using @rx.background on an event handler that polls for some condition and updates the user's state in response. Updating the state in a background task is a pushed update, by definition.

Let's say the client wants a push whenever the github zen API changes:

import asyncio
import httpx

import reflex as rx


ZEN_API = "https://api.github.com/zen"
LATEST_ZEN = "Life is short (so use Python)"
UPDATE_TASK = None


async def periodic_update_github_zen():
    global LATEST_ZEN

    a…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@thevicchij
Comment options

Answer selected by PasqualePuzio
Comment options

You must be logged in to vote
1 reply
@giovanni-steerable-co
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants