Skip to main content

A real-time async WebSocket networking engine for Kivy.

Project description

Kivy Network 🌐

PyPI version Python versions License: MIT

A robust, asynchronous, real-time WebSocket networking engine specifically designed for Kivy applications.

Building real-time multiplayer games, chat applications, or live dashboards in Kivy can be tricky because blocking network calls freeze the Kivy UI thread. kivy-network solves this by bridging the gap between Python's asyncio WebSockets and Kivy's 60FPS Clock, ensuring your app stays perfectly smooth.

Features

  • Thread-Safe UI Updates: Safely routes background network events into Kivys main thread using Clock.schedule_once.
  • Indestructible Client: Built-in auto-reconnect engine with backoff. Survives network drops, tunnels, and server reboots automatically.
  • Pro-Level Typing: Uses strict Python dataclasses (NetworkMessage) for full IDE autocomplete support. Say goodbye to dictionary typo bugs.
  • Ghost Hunter: Built-in Ping/Pong heartbeat system safely cleans up dead or half-open connections.
  • Lightweight: Minimal dependencies. Built purely on standard websockets and Kivy.

Installation

Install via pip or uv:

pip install kivy-network

(Note: Ensure you have Kivy installed and configured for your operating system).


Quick Start

1. The Kivy Client

Here is how easy it is to drop kivy-network into your application.

import asyncio
from kivy.app import App
from kivy.uix.label import Label
from kivy-network.network_client import RealTimeClient

class MultiplayerApp(App):
    def build(self):
        self.label = Label(text="Connecting...")

        self.network = RealTimeClient("ws://localhost:8765")

        self.network.bind(on_connected=self.on_connect)
        self.network.bind(on_message=self.on_message)
        self.network.bind(on_disconnected=self.on_disconnect)

        asyncio.create_task(self.network.run_forever())

        return self.label

    def on_connect(self, instance):
        self.label.text = "Connected! Joining room..."
        asyncio.create_task(self.network.join_room("lobby"))

    def on_message(self, instance, message):
        if message.type == "message":
            self.label.text = f"User {message.sender_id} says: {message.content}"

    def on_disconnect(self, instance):
        self.label.text = "Connection lost. Reconnecting..."

if __name__ == "__main__":
    asyncio.run(MultiplayerApp().async_run(async_lib='asyncio'))

2. The NetworkMessage API

Every time your UI receives an on_message event, it receives a strictly-typed NetworkMessage dataclass with the following properties available for autocomplete:

  • message.type (str) - E.g., 'welcome', 'message', 'admin'
  • message.room (str | None)
  • message.content (str | None)
  • message.sender_id (str | None)
  • message.raw_data (dict) - The raw JSON payload fallback.

Running a Test Server

kivy-network connects seamlessly to any standard WebSocket server.

We recommend spinning up a quick python server using the websockets library:

import asyncio
import websockets

async def echo(websocket):
    async for message in websocket:
        await websocket.send(message)

async def main():
    async with websockets.serve(echo, "localhost", 8765):
        await asyncio.Future()

asyncio.run(main())

Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.

License

MIT


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kivy_network-0.1.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kivy_network-0.1.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file kivy_network-0.1.1.tar.gz.

File metadata

  • Download URL: kivy_network-0.1.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for kivy_network-0.1.1.tar.gz
Algorithm Hash digest
SHA256 dafec525e7d0e010ec38b897645b74184d91746efe3097ce37b3c50cda48e775
MD5 d980fbe7f054143a1e7040f1ab6a2077
BLAKE2b-256 ea646df7c89dc2ca80a0603b183d1dda99677fa7e34f3d0d5c2e1056c2a67f9f

See more details on using hashes here.

File details

Details for the file kivy_network-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: kivy_network-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for kivy_network-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f31401d88968b8f39710a1ba5c4b7372355ecd2453da8f15e42143c0796fa416
MD5 af6aa2f8deb6e699cb73fec66609c9ef
BLAKE2b-256 a8fa5b76f1a1ece6ab3aa8313b46d4e02fad8274cb39a05e917bb47e3bdf73f7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page