DomsyncServer#

class domsync.domsync_server.DomsyncServer(connection_handler, host, port, verbose=True, root_id='domsync_root_id')#

domsync.domsync_server.DomsyncServer is a websocket server. When a client connects, it creates a domsync.Document instance for the client and calls the connection_handler function that was provided on initialisation (see below). The client’s document can be accessed with get_document(). After the document has been manipulated and flush() is called, the Javascript code that got generated by the document as a result of the changes is sent to the client where it gets applied to the Browser DOM and the changes appear on the screen. Any callback methods that were registered on the client’s document with domsync.core._Element.addEventListener() are also handled by the server: whenever those methods trigger ws_send on the client side, the message containig the event is received by the server and the corresponding Python callback function is triggered.

Parameters
  • connection_handler (Callable(domsync.domsync_server.DomsyncServer, WebSocketServerProtocol)) – is a callback function that is called each time a client connects to the server. It has two arguments: the first contains the DomsyncServer instance, the second contains the client websocket connection instance.

  • host (str) – host name to listen on

  • port (int) – port number to listen on

  • verbose (bool) – optional, should we print status messages to stdout? default = True.

  • root_id (str) – optional, id of the element in the client-side HTML where domsync should be rendered. default = ‘domsync_root_id’.

await close()#

stops the server

Returns

None

await flush(client)#

sends the generated Javascript code updates to the client that happened due to manipulations to the client’s document since the last time this function was called.

it is recommended to check domsync.domsync_server.DomsyncServer.is_connected() immediately before calling this function because this function throws an exception if the client has disconnected

Parameters

client – the client to send the updates for

Type

client: WebSocketServerProtocol

Returns

None

get_clients()#
Returns

list of connected clients

Return type

list of WebSocketServerProtocol

get_document(client)#

returns the document of the given client

it is recommended to check domsync.domsync_server.DomsyncServer.is_connected() immediately before calling this function because this function throws an exception if the client has disconnected

Parameters

client – a websocket client connection instance

Type

client: WebSocketServerProtocol

Returns

the document associated with the client connection

Return type

domsync.Document

is_connected(client)#

returns whether the given client is still connected

Parameters

client – a websocket client connection instance

Type

client: WebSocketServerProtocol

Returns

True if the client is still connected, False otherwise

Return type

bool

await serve()#

starts the server

Returns

None