Emit an update to the client. Args: update: The state update to send. token: The client token (tab) associated with the event.
(self, update: StateUpdate, token: str)
| 1674 | return None |
| 1675 | |
| 1676 | async def emit_update(self, update: StateUpdate, token: str) -> None: |
| 1677 | """Emit an update to the client. |
| 1678 | |
| 1679 | Args: |
| 1680 | update: The state update to send. |
| 1681 | token: The client token (tab) associated with the event. |
| 1682 | """ |
| 1683 | socket_record = self._token_manager.token_to_socket.get(token) |
| 1684 | if ( |
| 1685 | socket_record is None |
| 1686 | or socket_record.instance_id != self._token_manager.instance_id |
| 1687 | ): |
| 1688 | if isinstance(self._token_manager, RedisTokenManager): |
| 1689 | # The socket belongs to another instance of the app, send it to the lost and found. |
| 1690 | await self._token_manager.emit_lost_and_found(token, update) |
| 1691 | else: |
| 1692 | # If the socket record is None, we are not connected to a client. Prevent sending |
| 1693 | # updates to all clients. |
| 1694 | console.warn( |
| 1695 | f"Attempting to send delta to disconnected client {token!r}" |
| 1696 | ) |
| 1697 | return |
| 1698 | # Creating a task prevents the update from being blocked behind other coroutines. |
| 1699 | await asyncio.create_task( |
| 1700 | self.emit(str(constants.SocketEvent.EVENT), update, to=socket_record.sid), |
| 1701 | name=f"reflex_emit_event|{token}|{socket_record.sid}|{time.time()}", |
| 1702 | ) |
| 1703 | |
| 1704 | async def on_event(self, sid: str, data: Any): |
| 1705 | """Event for receiving front-end websocket events. |