{"id":18466517,"url":"https://github.com/graphql-python/graphql-ws","last_synced_at":"2025-04-12T11:52:00.384Z","repository":{"id":26356407,"uuid":"108325171","full_name":"graphql-python/graphql-ws","owner":"graphql-python","description":"GraphQL websockets","archived":false,"fork":false,"pushed_at":"2024-01-12T15:01:09.000Z","size":227,"stargazers_count":272,"open_issues_count":26,"forks_count":89,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-04-03T11:11:15.030Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/graphql-python.png","metadata":{"files":{"readme":"README.rst","changelog":"HISTORY.rst","contributing":"CONTRIBUTING.rst","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.rst","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-25T20:56:30.000Z","updated_at":"2024-11-02T11:48:57.000Z","dependencies_parsed_at":"2024-06-18T14:03:43.135Z","dependency_job_id":null,"html_url":"https://github.com/graphql-python/graphql-ws","commit_stats":{"total_commits":153,"total_committers":12,"mean_commits":12.75,"dds":0.4575163398692811,"last_synced_commit":"7ef25ecfaf5390bf1a0cb4023272d8cb074368c2"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-python%2Fgraphql-ws","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-python%2Fgraphql-ws/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-python%2Fgraphql-ws/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-python%2Fgraphql-ws/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graphql-python","download_url":"https://codeload.github.com/graphql-python/graphql-ws/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248565006,"owners_count":21125413,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T09:16:37.868Z","updated_at":"2025-04-12T11:52:00.350Z","avatar_url":"https://github.com/graphql-python.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"==========\nGraphQL WS\n==========\n\nWebsocket backend for GraphQL subscriptions.\n\nSupports the following application servers:\n\nPython 3 application servers, using asyncio:\n\n    * `aiohttp`_\n    * `websockets compatible servers`_ such as Sanic\n      (via `websockets \u003chttps://github.com/aaugustin/websockets/\u003e`__ library)\n    * `Django v2+`_\n\nPython 2 application servers:\n\n    * `Gevent compatible servers`_ such as Flask\n    * `Django v1.x`_\n      (via `channels v1.x \u003chttps://channels.readthedocs.io/en/1.x/inshort.html\u003e`__)\n\n\nInstallation instructions\n=========================\n\nFor instaling graphql-ws, just run this command in your shell\n\n.. code:: bash\n\n    pip install graphql-ws\n\n\nExamples\n========\n\nPython 3 servers\n----------------\n\nCreate a subscribable schema like this:\n\n.. code:: python\n\n    import asyncio\n    import graphene\n\n\n    class Query(graphene.ObjectType):\n        hello = graphene.String()\n\n        @staticmethod\n        def resolve_hello(obj, info, **kwargs):\n            return \"world\"\n\n\n    class Subscription(graphene.ObjectType):\n        count_seconds = graphene.Float(up_to=graphene.Int())\n\n        async def resolve_count_seconds(root, info, up_to):\n            for i in range(up_to):\n                yield i\n                await asyncio.sleep(1.)\n            yield up_to\n\n\n    schema = graphene.Schema(query=Query, subscription=Subscription)\n\naiohttp\n~~~~~~~\n\nThen just plug into your aiohttp server.\n\n.. code:: python\n\n    from graphql_ws.aiohttp import AiohttpSubscriptionServer\n    from .schema import schema\n\n    subscription_server = AiohttpSubscriptionServer(schema)\n\n\n    async def subscriptions(request):\n        ws = web.WebSocketResponse(protocols=('graphql-ws',))\n        await ws.prepare(request)\n\n        await subscription_server.handle(ws)\n        return ws\n\n\n    app = web.Application()\n    app.router.add_get('/subscriptions', subscriptions)\n\n    web.run_app(app, port=8000)\n\nYou can see a full example here:\nhttps://github.com/graphql-python/graphql-ws/tree/master/examples/aiohttp\n\n\nwebsockets compatible servers\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nWorks with any framework that uses the websockets library for its websocket\nimplementation. For this example, plug in your Sanic server.\n\n.. code:: python\n\n    from graphql_ws.websockets_lib import WsLibSubscriptionServer\n    from . import schema\n\n    app = Sanic(__name__)\n\n    subscription_server = WsLibSubscriptionServer(schema)\n\n    @app.websocket('/subscriptions', subprotocols=['graphql-ws'])\n    async def subscriptions(request, ws):\n        await subscription_server.handle(ws)\n        return ws\n\n\n    app.run(host=\"0.0.0.0\", port=8000)\n\n\nDjango v2+\n~~~~~~~~~~\n\n\nDjango Channels 2\n~~~~~~~~~~~~~~~~~\n\nSet up with Django Channels just takes three steps:\n\n1. Install the apps\n2. Set up your schema\n3. Configure the channels router application\n\nFirst ``pip install channels`` and it to your ``INSTALLED_APPS``. If you\nwant graphiQL, install the ``graphql_ws.django`` app before\n``graphene_django`` to serve a graphiQL template that will work with\nwebsockets:\n\n.. code:: python\n\n    INSTALLED_APPS = [\n        \"channels\",\n        \"graphql_ws.django\",\n        \"graphene_django\",\n        # ...\n    ]\n\nPoint to your schema in Django settings:\n\n.. code:: python\n\n    GRAPHENE = {\n        'SCHEMA': 'yourproject.schema.schema'\n    }\n\nFinally, you can set up channels routing yourself (maybe using\n``graphql_ws.django.routing.websocket_urlpatterns`` in your\n``URLRouter``), or you can just use one of the preset channels\napplications:\n\n.. code:: python\n\n    ASGI_APPLICATION = 'graphql_ws.django.routing.application'\n    # or\n    ASGI_APPLICATION = 'graphql_ws.django.routing.auth_application'\n\nRun ``./manage.py runserver`` and go to\n`http://localhost:8000/graphql \u003chttp://localhost:8000/graphql\u003e`__ to test!\n\n\nPython 2  servers\n-----------------\n\nCreate a subscribable schema like this:\n\n.. code:: python\n\n    import graphene\n    from rx import Observable\n\n\n    class Query(graphene.ObjectType):\n        hello = graphene.String()\n\n        @staticmethod\n        def resolve_hello(obj, info, **kwargs):\n            return \"world\"\n\n\n    class Subscription(graphene.ObjectType):\n        count_seconds = graphene.Float(up_to=graphene.Int())\n\n        async def resolve_count_seconds(root, info, up_to=5):\n            return Observable.interval(1000)\\\n                             .map(lambda i: \"{0}\".format(i))\\\n                             .take_while(lambda i: int(i) \u003c= up_to)\n\n\n    schema = graphene.Schema(query=Query, subscription=Subscription)\n\nGevent compatible servers\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThen just plug into your Gevent server, for example, Flask:\n\n.. code:: python\n\n    from flask_sockets import Sockets\n    from graphql_ws.gevent import GeventSubscriptionServer\n    from schema import schema\n\n    subscription_server = GeventSubscriptionServer(schema)\n    app.app_protocol = lambda environ_path_info: 'graphql-ws'\n\n\n    @sockets.route('/subscriptions')\n    def echo_socket(ws):\n        subscription_server.handle(ws)\n        return []\n\nYou can see a full example here:\nhttps://github.com/graphql-python/graphql-ws/tree/master/examples/flask_gevent\n\nDjango v1.x\n~~~~~~~~~~~\n\nFor Django v1.x and Django Channels v1.x, setup your schema in ``settings.py``\n\n.. code:: python\n\n    GRAPHENE = {\n        'SCHEMA': 'yourproject.schema.schema'\n    }\n\nThen ``pip install \"channels\u003c1\"`` and it to your django apps, adding the\nfollowing to your ``settings.py``\n\n.. code:: python\n\n    CHANNELS_WS_PROTOCOLS = [\"graphql-ws\", ]\n    CHANNEL_LAYERS = {\n        \"default\": {\n            \"BACKEND\": \"asgiref.inmemory.ChannelLayer\",\n            \"ROUTING\": \"django_subscriptions.urls.channel_routing\",\n        },\n    }\n\nAnd finally add the channel routes\n\n.. code:: python\n\n    from channels.routing import route_class\n    from graphql_ws.django_channels import GraphQLSubscriptionConsumer\n\n    channel_routing = [\n        route_class(GraphQLSubscriptionConsumer, path=r\"^/subscriptions\"),\n    ]\n\nYou can see a full example here:\nhttps://github.com/graphql-python/graphql-ws/tree/master/examples/django_subscriptions\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-python%2Fgraphql-ws","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraphql-python%2Fgraphql-ws","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-python%2Fgraphql-ws/lists"}