server.websocket_server

File:

EuljiroWorship/server/websocket_server.py

Author:

Benjamin Jaedon Choi - https://github.com/saintbenjamin

Affiliated Church:

The Eulji-ro Presbyterian Church [대한예수교장로회(통합) 을지로교회]

Address:

The Eulji-ro Presbyterian Church, 24-10, Eulji-ro 20-gil, Jung-gu, Seoul 04549, South Korea

Telephone:

+82-2-2266-3070

E-mail:

euljirochurch [at] G.M.A.I.L. (replace [at] with @ and G.M.A.I.L as you understood.)

License:

MIT License with Attribution Requirement (see LICENSE file for details); Copyright (c) 2025 The Eulji-ro Presbyterian Church.

WebSocket server for real-time slide broadcasting in EuljiroWorship.

This implements an aiohttp-based WebSocket server responsible for real-time communication with overlay clients during worship services. It receives slide data as JSON messages and broadcasts them to all connected clients.

The server is designed to be robust in live worship environments and provides:

  • Real-time broadcast fan-out to multiple connected clients

  • Ping/pong heartbeat handling to detect dead connections

  • Error isolation per client connection

  • Automatic cleanup of disconnected or faulty (“zombie”) clients

  • Graceful shutdown with proper socket closure

The WebSocket endpoint listens on /ws and expects incoming messages to be JSON representations of slide data, which are then forwarded to all connected overlay clients.

async server.websocket_server.websocket_handler(request)[source]

Handle a single WebSocket client connection.

The handler:
  • upgrades the incoming HTTP request to a WebSocket

  • registers the client in the global client set

  • responds to heartbeat “ping” messages with “pong”

  • attempts to parse incoming text frames as JSON and broadcast them

  • ensures the client is removed and closed on disconnect/errors

Parameters:

request (aiohttp.web.Request) – Incoming request for WebSocket upgrade.

Returns:

The prepared WebSocket response object.

Return type:

aiohttp.web.WebSocketResponse

async server.websocket_server.broadcast(slide_dict)[source]

Broadcast a slide payload to all currently connected WebSocket clients.

The payload is JSON-serialized and sent as a text frame. The function also performs basic client hygiene:

  • detects and removes closed or errored (“zombie”) connections

  • removes clients that fail to receive within a short timeout

Parameters:

slide_dict (dict) – Slide payload to broadcast. Must be JSON-serializable.

Returns:

None

async server.websocket_server.on_shutdown(app)[source]

Close all active WebSocket connections during server shutdown.

This coroutine is registered to app.on_shutdown and is expected to run when aiohttp is stopping. It attempts to close all active client sockets with a normal “going away” code.

Parameters:

app (aiohttp.web.Application) – The aiohttp application instance.

Returns:

None

server.websocket_server.main(port=8765)[source]

Start the WebSocket server.

Parameters:

port (int, optional) – TCP port exposed for incoming WebSocket connections. Defaults to 8765.

Returns:

None

Return type:

None