websockets.server

Attributes

CONNECTIONS

TOKEN

NOTFOUND

SESSIONS

STAFF_CONNECTIONS

STAFF

ACTIVE_CHATS

CHANNELS

Classes

WebSocketServer

A websocket server connection.

Functions

get_payload(→ JSONObject | None)

Deserialize JSON payload and check type.

error(→ None)

Sends an error.

acknowledge(→ None)

Sends an acknowledge.

handle_listen(→ None)

Handles listening clients.

handle_authentication(→ None)

Handles authentication.

handle_status(→ None)

Handles status requests.

handle_broadcast(→ None)

Handles broadcasts.

handle_manage(→ None)

Handles managing clients.

handle_customer_chat(→ None)

Starts a chat. Handles listening to messages on channel.

handle_staff_chat(→ None)

Registers staff member and listens to messages.

handle_start(→ None)

main(→ None)

Module Contents

websockets.server.CONNECTIONS: dict[str, set[websockets.legacy.server.WebSocketServerProtocol]][source]
websockets.server.TOKEN = ''[source]
websockets.server.NOTFOUND[source]
websockets.server.SESSIONS: dict[str, sqlalchemy.orm.Session][source]
websockets.server.STAFF_CONNECTIONS: dict[str, set[websockets.legacy.server.WebSocketServerProtocol]][source]
websockets.server.STAFF: dict[str, dict[str, onegov.user.User]][source]
websockets.server.ACTIVE_CHATS: dict[str, dict[uuid.UUID, onegov.chat.models.Chat]][source]
websockets.server.CHANNELS: dict[str, dict[str, set[websockets.legacy.server.WebSocketServerProtocol]]][source]
class websockets.server.WebSocketServer(config: onegov.server.config.Config, session_manager: onegov.core.orm.SessionManager, *args: Any, **kwargs: Any)[source]

Bases: websockets.legacy.server.WebSocketServerProtocol

A websocket server connection.

This protocol handles multiple websocket applications: - Ticket notifications - Ticker - Chat

Chat behaves differently from the others and will eventually be carved out into a separate service. To not interfere with any existing functionality, we try to refrain from making backwards-incompatible changes. That way, ticker and notifications should continue to work without any modification.

TODO: Move chat to a dedicated service.

schema: str[source]
user_id: str | None[source]
signed_session_id: str | None[source]
config[source]
session_manager[source]
async process_request(path: str, headers: websockets.Headers) HTTPResponse | None[source]

Intercept initial HTTP request.

Before establishing a WebSocket connection, a client sends a HTTP request to “upgrade” the connection to a WebSocket connection.

Chat

We authenticate the user before creating the WebSocket connection. The user is identified based on the session cookie. In addition to the cookie, we require a one-time token that the user must have obtained prior to requesting the WebSocket connection.

populate_staff() None[source]

Populate staff users.

async get_chat(id: uuid.UUID) onegov.chat.models.Chat[source]
async update_database() None[source]
unsign(text: str) str | None[source]

Unsigns a signed text, returning None if unsuccessful.

property session: sqlalchemy.orm.Session[source]
property application_id_hash: str[source]

The application_id as hash, use this if the application_id can be read by the user -> this obfuscates things slightly.

property session_cache: onegov.core.cache.RedisCacheRegion[source]

A cache that is kept for a long-ish time.

property namespace: str[source]
property application_id: str[source]
property application_config: dict[str, Any][source]
property browser_session: BrowserSession | dict[str, Any][source]
websockets.server.get_payload(message: str | bytes, expected: Collection[str]) JSONObject | None[source]

Deserialize JSON payload and check type.

async websockets.server.error(websocket: websockets.legacy.server.WebSocketServerProtocol, message: str, close: bool = True) None[source]

Sends an error.

async websockets.server.acknowledge(websocket: websockets.legacy.server.WebSocketServerProtocol) None[source]

Sends an acknowledge.

async websockets.server.handle_listen(websocket: websockets.legacy.server.WebSocketServerProtocol, payload: onegov.core.types.JSONObject_ro) None[source]

Handles listening clients.

async websockets.server.handle_authentication(websocket: websockets.legacy.server.WebSocketServerProtocol, payload: onegov.core.types.JSONObject_ro) None[source]

Handles authentication.

async websockets.server.handle_status(websocket: websockets.legacy.server.WebSocketServerProtocol, payload: onegov.core.types.JSONObject_ro) None[source]

Handles status requests.

async websockets.server.handle_broadcast(websocket: websockets.legacy.server.WebSocketServerProtocol, payload: onegov.core.types.JSONObject_ro) None[source]

Handles broadcasts.

async websockets.server.handle_manage(websocket: websockets.legacy.server.WebSocketServerProtocol, authentication_payload: onegov.core.types.JSONObject_ro) None[source]

Handles managing clients.

async websockets.server.handle_customer_chat(websocket: WebSocketServer, payload: onegov.core.types.JSONObject_ro) None[source]

Starts a chat. Handles listening to messages on channel.

async websockets.server.handle_staff_chat(websocket: WebSocketServer, payload: onegov.core.types.JSONObject_ro) None[source]

Registers staff member and listens to messages.

async websockets.server.handle_start(websocket: websockets.legacy.server.WebSocketServerProtocol) None[source]
async websockets.server.main(host: str, port: int, token: str, config: Config | None = None) None[source]