Server
elva.server
Module containing server components.
Classes:
-
RequestProcessor–Collector class of HTTP request processing functions.
-
Room–Connection handler for one Y Document following the Yjs protocol.
-
WebsocketServer–Serving component using
Roomas internal connection handler.
Functions:
-
free_tcp_port–Let the OS select a free TCP port for IPv4 addresses.
Attributes:
-
RE_IDENTIFIER–Regular expression for a valid Y Doc identifier.
-
RoomState–The states of a
Roomcomponent. -
WebsocketServerState–The states of a
WebsocketServercomponent.
RE_IDENTIFIER = re.compile('^[A-Za-z0-9\\-_]{10,250}$')
module-attribute
Regular expression for a valid Y Doc identifier.
RoomState = create_component_state('RoomState')
module-attribute
The states of a Room component.
WebsocketServerState = create_component_state('WebsocketServerState', ('SERVING',))
module-attribute
The states of a WebsocketServer component.
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 | Response–Noneif no processing functions returned anything, or the firstResponsereturned.
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
RUNNINGstate is set. -
cleanup–Hook running after the component got cancelled and before it states become unset to
NONE. -
add–Add a client connection.
-
remove–Remove a client connection.
-
broadcast–Broadcast
datato all clients exceptclient. -
process–Process incoming messages from
client. -
process_sync_step1–Process a sync step 1 payload
statefromclient. -
process_sync_update–Process a sync update message payload
updatefromclient. -
process_awareness–Process an awareness message payload
statefromclient.
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[ServerConnection]) –Set of active connections.
-
states(RoomState) –The states this component can have.
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.
states
property
The states this component can have.
before()
async
Hook runnig before the RUNNING state is set.
Used to start the Y Document store.
cleanup()
async
Hook running after the component got cancelled and before it states become unset to NONE.
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(ServerConnection) –connection to add the list of connections.
Source code in src/elva/server.py
remove(client)
Remove a client connection.
Parameters:
-
client(ServerConnection) –connection to remove from the list of connections.
broadcast(data, client)
Broadcast data to all clients except client.
Parameters:
-
data(bytes) –data to send.
-
client(ServerConnection) –connection from which
datacame 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(ServerConnection) –connection from which
datawas 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(ServerConnection) –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(ServerConnection) –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.
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.
-
cleanup–Hook running on cancellation and before the component unsets its states to
NONE. -
wait_for_room_closed–Wait for a room corresponding to given identifier to stop.
-
check_path–Check if a request path is valid.
-
get_room–Get or create a
Roomvia its correspondingidentifier. -
handle–Handle a
websocketconnection.
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.
-
states(WebsocketServerState) –The states this component can have.
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.
states
property
The states this component can have.
run()
async
Hook handling incoming connections and messages.
Source code in src/elva/server.py
cleanup()
async
Hook running on cancellation and before the component unsets its states to NONE.
It waits for all active rooms being stopped.
Source code in src/elva/server.py
wait_for_room_closed(identifier)
async
Wait for a room corresponding to given identifier to stop.
Parameters:
-
identifier(str) –the identifier to which the room belongs.
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.
Returns:
-
None | Response–Noneif an identifier was given, else aResponsewith HTTP status 403 (forbidden).
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(ServerConnection) –connection from data are being received.
Source code in src/elva/server.py
free_tcp_port(host=None)
Let the OS select a free TCP port for IPv4 addresses.
Parameters:
-
host(None | str, default:None) –the interface to search a free TCP port on.
Returns:
-
int–a recently free tcp port.