-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Broadcast server awareness to all clients #73
Changes from 17 commits
e7b3979
4d00a7d
8bd9703
0f4e085
effe236
5bf4798
4a5a71f
e522c8a
5eb8316
f01c2cc
ec1ef6b
7b2dc09
9a4b71f
52c80ed
1df8f38
c4a2392
2578840
84e41dd
b656c2e
9960536
180763b
ec0fe78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,7 +4,7 @@ | |||||||||||
from functools import partial | ||||||||||||
from inspect import isawaitable | ||||||||||||
from logging import Logger, getLogger | ||||||||||||
from typing import Awaitable, Callable | ||||||||||||
from typing import Any, Awaitable, Callable | ||||||||||||
|
||||||||||||
from anyio import ( | ||||||||||||
TASK_STATUS_IGNORED, | ||||||||||||
|
@@ -16,16 +16,17 @@ | |||||||||||
from anyio.abc import TaskGroup, TaskStatus | ||||||||||||
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream | ||||||||||||
from pycrdt import ( | ||||||||||||
Awareness, | ||||||||||||
Doc, | ||||||||||||
Subscription, | ||||||||||||
YMessageType, | ||||||||||||
YSyncMessageType, | ||||||||||||
create_awareness_message, | ||||||||||||
create_sync_message, | ||||||||||||
create_update_message, | ||||||||||||
handle_sync_message, | ||||||||||||
) | ||||||||||||
|
||||||||||||
from .awareness import Awareness | ||||||||||||
from .websocket import Websocket | ||||||||||||
from .ystore import BaseYStore | ||||||||||||
from .yutils import put_updates | ||||||||||||
|
@@ -77,11 +78,12 @@ def __init__( | |||||||||||
ydoc: An optional document for the room (a new one is created otherwise). | ||||||||||||
""" | ||||||||||||
self.ydoc = Doc() if ydoc is None else ydoc | ||||||||||||
self.awareness = Awareness(self.ydoc) | ||||||||||||
self.ready_event = Event() | ||||||||||||
self.ready = ready | ||||||||||||
self.ystore = ystore | ||||||||||||
self.log = log or getLogger(__name__) | ||||||||||||
self.awareness = Awareness(self.ydoc) | ||||||||||||
self.awareness.observe(self.local_update_awareness) | ||||||||||||
self.clients = set() | ||||||||||||
self._on_message = None | ||||||||||||
self.exception_handler = exception_handler | ||||||||||||
|
@@ -304,3 +306,33 @@ async def serve(self, websocket: Websocket): | |||||||||||
self.clients.remove(websocket) | ||||||||||||
except Exception as exception: | ||||||||||||
self._handle_exception(exception) | ||||||||||||
|
||||||||||||
def local_update_awareness(self, type: str, changes: tuple[dict[str, Any], Any]) -> None: | ||||||||||||
""" | ||||||||||||
Callback to broadcast the server awareness to clients. | ||||||||||||
""" | ||||||||||||
brichet marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
if not changes[1] == "local": | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
return | ||||||||||||
|
||||||||||||
if self._task_group is not None: | ||||||||||||
brichet marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
updated_clients = [ | ||||||||||||
*changes[0].get("added", []), | ||||||||||||
*changes[0].get("filtered_updated", []), | ||||||||||||
] | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes consist of
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I added a test to broadcast only if there is an effective change (not 'update'). |
||||||||||||
state = self.awareness.encode_awareness_update(updated_clients) | ||||||||||||
message = create_awareness_message(state) | ||||||||||||
self._task_group.start_soon(self._local_update_awareness, message) | ||||||||||||
brichet marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
else: | ||||||||||||
self.log.error("Cannot broadcast server awareness: YRoom not started") | ||||||||||||
|
||||||||||||
async def _local_update_awareness(self, state: bytes) -> None: | ||||||||||||
brichet marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
try: | ||||||||||||
async with create_task_group() as tg: | ||||||||||||
for client in self.clients: | ||||||||||||
self.log.debug( | ||||||||||||
"Sending awareness from server to client with endpoint: %s", | ||||||||||||
client.path, | ||||||||||||
) | ||||||||||||
tg.start_soon(client.send, state) | ||||||||||||
except Exception as e: | ||||||||||||
self.log.error("Error while broadcasting awareness changes: %s", e) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -30,7 +30,7 @@ classifiers = [ | |||||
dependencies = [ | ||||||
"anyio >=3.6.2,<5", | ||||||
"sqlite-anyio >=0.2.3,<0.3.0", | ||||||
"pycrdt >=0.9.16,<0.10.0", | ||||||
"pycrdt >=0.10.0,<0.11.0", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's take the latest bugfix.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it is required for this change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, sorry again for the confusion. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I'll merge then. |
||||||
] | ||||||
|
||||||
[project.optional-dependencies] | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the callback name
local_update_awareness
, maybesend_server_awareness
?