{"library":"pycrdt-websocket","title":"Pycrdt WebSocket Connector","description":"Pycrdt-websocket provides a WebSocket connector for `pycrdt`, enabling real-time, collaborative document synchronization using Y-CRDTs over a network. It facilitates both server-side handling of Y-CRDT documents and client-side connections. The library is actively maintained with frequent minor releases, typically addressing bug fixes and compatibility updates.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install pycrdt-websocket"],"cli":null},"imports":["from pycrdt.websocket import WebSocketServer","from pycrdt.websocket import YDocRoom","from pycrdt_store import YStore"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import asyncio\nfrom pycrdt import YDoc, YText\nfrom pycrdt.websocket import WebSocketServer, YDocRoom\n\nasync def server_main():\n    # Initialize a Y-CRDT document for the server\n    ydoc = YDoc()\n    with ydoc.transaction():\n        text = ydoc.get_text('my_text')\n        text += 'Hello from server!'\n\n    # Create a WebSocket server that manages rooms\n    server = WebSocketServer(ydoc, 'ws://127.0.0.1:1234')\n    print(\"Server starting on ws://127.0.0.1:1234\")\n    await server.start()\n    await asyncio.sleep(5) # Keep server running for a bit\n    await server.stop()\n    print(\"Server stopped\")\n\nasync def client_main():\n    await asyncio.sleep(1) # Give server a moment to start\n    print(\"Client connecting...\")\n    # Connect to the server with a YDocRoom\n    client_ydoc = YDoc()\n    room = YDocRoom(client_ydoc, 'ws://127.0.0.1:1234')\n    await room.connect()\n\n    # Access the shared text\n    shared_text = client_ydoc.get_text('my_text')\n    print(f\"Client received: '{shared_text}'\")\n\n    with client_ydoc.transaction():\n        shared_text.append(' And client!')\n    print(f\"Client sent: '{shared_text}'\")\n\n    await asyncio.sleep(1) # Allow changes to propagate\n    await room.disconnect()\n    print(\"Client disconnected\")\n\nasync def main():\n    server_task = asyncio.create_task(server_main())\n    client_task = asyncio.create_task(client_main())\n    await asyncio.gather(server_task, client_task)\n\nif __name__ == '__main__':\n    asyncio.run(main())\n","lang":"python","description":"This quickstart demonstrates a basic pycrdt-websocket setup with a server and a client. The server initializes a YDoc and listens for connections, while the client connects to synchronize its own YDoc. It shows how text changes are synchronized in real-time.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}