by nprateem · 1y ago · 0 comments
async def redirect_from_backend():
yield SSE.patch_elements('<div id="indicator">Redirecting in 3 seconds...</div>')
await asyncio.sleep(3)
yield SSE.execute_script('window.location = "/guide"')
LOL, they really think it's a good use of server resources to sleep so the client doesn't have to.[1] https://data-star.dev/how_tos/redirect_the_page_from_the_bac...
It’s not like synchronous servers e.g. Flask with Gunicorn (in the Python world) where you have a few pre-forked server processes. In that case it would be a waste to prevent one of those processes from serving other requests while sleeping, I agree.
Ultimately it’s cool though to be able to control the client from the backend asynchronously, updating signals (think of them as a kind of special front-end variable with superpowers) when something on the backend happens, or updating the client’s DOM, or running scripts.
This approach completely breaks sensible reasoning about what clients are responsible for. What's next, sending every keystroke to the server so it can tell the client to update a text input?!?
It still takes resources to keep SSEs open, load balance, reopen lost connections, etc. But worst of all is shifting fundamental logic back to the server for literally no benefit.