Server
elva.server
Websocket server classes.
Classes:
-
RequestProcessor
–Collector class of HTTP request processing functions.
-
Room
–Connection handler for one Y Document following the Yjs protocol.
-
ElvaRoom
–Connection handler for one Y Document following the ELVA protocol.
-
WebsocketServer
–Serving component using
Room
as internal connection handler. -
ElvaWebsocketServer
–Serving component using
ElvaRoom
as internal connection handler.
RequestProcessor(*funcs)
Collector class of HTTP request processing functions.
Parameters:
Methods:
-
process_request
–Process a HTTP request for given functions.
Source code in src/elva/server.py
process_request(websocket, request)
Process a HTTP request for given functions.
This function is designed to be given to serve
.
Parameters:
-
websocket
(ServerConnection
) –connection object.
-
request
(Request
) –HTTP request header object.
Returns:
-
None | tuple[HTTPStatus, dict[str, str], None | bytes]
–Abort information like in
abort_basic_auth
on first occurence, elseNone
.
Source code in src/elva/server.py
Room(identifier, persistent=False, path=None)
Bases: Component
Connection handler for one Y Document following the Yjs protocol.
If persistent = False
and path = None
, messages will be broadcasted only.
Nothing is saved.
If persistent = True
and path = None
, a Y Document will be present in this room, saving all incoming Y updates in there. This happens only in volatile memory.
If persistent = True
and path = Path(to/some/directory)
, a Y Document will be present and its contents will be saved to disk under the given directory.
The name of the corresponding file is derived from identifier
.
Parameters:
-
identifier
(str
) –identifier for the used Y Document.
-
persistent
(bool
, default:False
) –flag whether to store received Y Document updates.
-
path
(None | Path
, default:None
) –path where to save a Y Document on disk.
Methods:
-
before
–Hook runnig before the
started
signal is set. -
cleanup
–Hook running after the component got cancelled and before its
stopped
signal is set. -
add
–Add a client connection.
-
remove
–Remove a client connection.
-
broadcast
–Broadcast
data
to all clients exceptclient
. -
process
–Process incoming messages from
client
. -
process_sync_step1
–Process a sync step 1 payload
state
fromclient
. -
process_sync_update
–Process a sync update message payload
update
fromclient
. -
process_awareness
–Process an awareness message payload
state
fromclient
.
Attributes:
-
path
(None | Path
) –Path where to save a Y Document on disk.
-
ydoc
(Doc
) –Y Document instance holding received updates.
-
store
(SQLiteStore
) –component responsible for writing received Y updates to disk.
-
identifier
(str
) –Identifier of the synchronized Y Document.
-
persistent
(bool
) –Flag whether to store received Y Document updates.
-
clients
(set[ClientConnection]
) –Set of active connections.
Source code in src/elva/server.py
path
instance-attribute
Path where to save a Y Document on disk.
ydoc
instance-attribute
Y Document instance holding received updates.
store
instance-attribute
component responsible for writing received Y updates to disk.
identifier = identifier
instance-attribute
Identifier of the synchronized Y Document.
persistent = persistent
instance-attribute
Flag whether to store received Y Document updates.
clients = set()
instance-attribute
Set of active connections.
before()
async
Hook runnig before the started
signal is set.
Used to start the Y Document store.
Source code in src/elva/server.py
cleanup()
async
Hook running after the component got cancelled and before its stopped
signal is set.
Used to close all client connections gracefully. The store is closed automatically and calls its cleanup method separately.
Source code in src/elva/server.py
add(client)
Add a client connection.
Parameters:
-
client
(ClientConnection
) –connection to add the list of connections.
Source code in src/elva/server.py
remove(client)
Remove a client connection.
Parameters:
-
client
(ClientConnection
) –connection to remove from the list of connections.
Source code in src/elva/server.py
broadcast(data, client)
Broadcast data
to all clients except client
.
Parameters:
-
data
(bytes
) –data to send.
-
client
(ClientConnection
) –connection from which
data
came and thus to exclude from broadcasting.
Source code in src/elva/server.py
process(data, client)
async
Process incoming messages from client
.
If persistent = False
, just call broadcast(data, client)
.
If persistent = True
, data
is assumed to be a Y message and tried to be decomposed.
On successful decomposition, actions are taken according to the Yjs protocol spec.
Parameters:
-
data
(bytes
) –data received from
client
. -
client
(ClientConnection
) –connection from which
data
was received.
Source code in src/elva/server.py
process_sync_step1(state, client)
async
Process a sync step 1 payload state
from client
.
Answer it with a sync step 2. Also, start a reactive cross-sync by answering with a sync step 1 additionally.
Parameters:
-
state
(bytes
) –payload of the received sync step 1 message from
client
. -
client
(ClientConnection
) –connection from which the sync step 1 message came.
Source code in src/elva/server.py
process_sync_update(update, client)
async
Process a sync update message payload update
from client
.
Apply the update to the internal ydoc
instance and broadcast the same update to all other clients than client
.
Parameters:
-
update
(bytes
) –payload of the received sync update message from
client
. -
client
(ClientConnection
) –connection from which the sync update message came.
Source code in src/elva/server.py
process_awareness(state, client)
async
Process an awareness message payload state
from client
.
Currently, this is implemented as a no-op.
Source code in src/elva/server.py
ElvaRoom(identifier, persistent, path)
Bases: Room
Connection handler for one Y Document following the ELVA protocol.
If persistent = False
and path = None
, messages will be broadcasted only.
Nothing is saved.
If persistent = True
and path = None
, a Y Document will be present in this room, saving all incoming Y updates in there. This happens only in volatile memory.
If persistent = True
and path = Path(to/some/directory)
, a Y Document will be present and its contents will be saved to disk under the given directory.
The name of the corresponding file is derived from identifier
.
Parameters:
-
identifier
(str
) –identifier for the used Y Document.
-
persistent
(bool
) –flag whether to store received Y Document updates.
-
path
(None | Path
) –path where to save a Y Document on disk.
Methods:
-
broadcast
–Broadcast
data
to all clients exceptclient
. -
process
–Process incoming messages from
client
. -
process_sync_step1
–Process a sync step 1 payload
state
fromclient
. -
process_sync_update
–Process a sync update message payload
update
fromclient
.
Attributes:
-
uuid
(bytes
) –As
ElvaMessage
encodedidentifier
.
Source code in src/elva/server.py
uuid
instance-attribute
As ElvaMessage
encoded identifier
.
broadcast(data, client)
Broadcast data
to all clients except client
.
Parameters:
-
data
(bytes
) –data to send.
-
client
(ClientConnection
) –connection from which
data
came and thus to exclude from broadcasting.
Source code in src/elva/server.py
process(data, client)
async
Process incoming messages from client
.
If persistent = False
, just call broadcast(data, client)
.
If persistent = True
, data
is assumed to be a Y message and tried to be decomposed.
On successful decomposition, actions are taken according to the Yjs protocol spec.
Parameters:
-
data
(bytes
) –data received from
client
. -
client
(ClientConnection
) –connection from which
data
was received.
Source code in src/elva/server.py
process_sync_step1(state, client)
async
Process a sync step 1 payload state
from client
.
Answer it with a sync step 2. Also, start a reactive cross-sync by answering with a sync step 1 additionally.
Parameters:
-
state
(bytes
) –payload of the received sync step 1 message from
client
. -
client
(ClientConnection
) –connection from which the sync step 1 message came.
Source code in src/elva/server.py
process_sync_update(update, client)
async
Process a sync update message payload update
from client
.
Apply the update to the internal self.ydoc
instance and broadcast the same update to all other clients than client
.
Parameters:
-
update
(bytes
) –payload of the received sync update message from
client
. -
client
(ClientConnection
) –connection from which the sync update message came.
Source code in src/elva/server.py
WebsocketServer(host, port, persistent=False, path=None, process_request=None)
Bases: Component
Serving component using Room
as internal connection handler.
Parameters:
-
host
(str
) –hostname or IP address to be published at.
-
port
(int
) –port to listen on.
-
persistent
(bool
, default:False
) –flag whether to save Y Document updates persistently.
-
path
(None | Path
, default:None
) –path where to store Y Document contents on disk.
-
process_request
(None | Callable
, default:None
) –callable checking the HTTP request headers on new connections.
Methods:
-
run
–Hook handling incoming connections and messages.
-
check_path
–Check if a request path is valid.
-
get_room
–Get or create a
Room
via its correspondingidentifier
. -
handle
–Handle a
websocket
connection.
Attributes:
-
process_request
(Callable
) –callable checking the HTTP request headers on new connections.
-
host
(str
) –hostname or IP address to be published at.
-
port
(int
) –port to listen on.
-
persistent
(bool
) –flag whether to save Y Document updates persistently.
-
path
(None | Path
) –path where to store Y Document contents on disk.
-
rooms
(dict[str, Room]
) –mapping of connection handlers to their corresponding identifiers.
Source code in src/elva/server.py
process_request
instance-attribute
callable checking the HTTP request headers on new connections.
host = host
instance-attribute
hostname or IP address to be published at.
port = port
instance-attribute
port to listen on.
persistent = persistent
instance-attribute
flag whether to save Y Document updates persistently.
path = path
instance-attribute
path where to store Y Document contents on disk.
rooms = dict()
instance-attribute
mapping of connection handlers to their corresponding identifiers.
run()
async
Hook handling incoming connections and messages.
Source code in src/elva/server.py
check_path(websocket, request)
Check if a request path is valid.
This function is a request processing callable and automatically passed to the inner serve
function.
Parameters:
-
websocket
(ServerConnection
) –connection object.
-
request
(Request
) –HTTP request header object.
Source code in src/elva/server.py
get_room(identifier)
async
Get or create a Room
via its corresponding identifier
.
Parameters:
-
identifier
(str
) –string identifiying the underlying Y Document.
Returns:
-
Room
–room to the given
identifier
.
Source code in src/elva/server.py
handle(websocket)
async
Handle a websocket
connection.
Upon connection, a room is provided, to which the data are given for further processing.
This methods is passed to serve
internally.
Parameters:
-
websocket
(ClientConnection
) –connection from data are being received.
Source code in src/elva/server.py
ElvaWebsocketServer(host, port, persistent=False, path=None, process_request=None)
Bases: WebsocketServer
Serving component using ElvaRoom
as internal connection handler.
Methods:
-
check_path
–Check if a request path is valid.
-
get_room
–Get or create an
ElvaRoom
via its correspondingidentifier
. -
handle
–Handle a
websocket
connection.
Source code in src/elva/server.py
check_path(websocket, request)
Check if a request path is valid.
This function is a request processing callable and automatically passed to the inner serve
function.
Parameters:
-
websocket
(ServerConnection
) –connection object.
-
request
(Request
) –HTTP request header object.
Source code in src/elva/server.py
get_room(identifier)
async
Get or create an ElvaRoom
via its corresponding identifier
.
Parameters:
-
identifier
(str
) –string identifiying the underlying Y Document.
Returns:
-
ElvaRoom
–room to the given
identifier
.
Source code in src/elva/server.py
handle(websocket)
async
Handle a websocket
connection.
Upon connection, an room is provided, to which the data are given for further processing.
This method is passed to serve
internally.
Parameters:
-
websocket
(ClientConnection
) –connection from data are being received.