{"id":16928685,"url":"https://github.com/boppreh/server-sent-events","last_synced_at":"2025-10-03T13:45:03.263Z","repository":{"id":15951919,"uuid":"18694439","full_name":"boppreh/server-sent-events","owner":"boppreh","description":"Python library for Server-Sent-Events","archived":false,"fork":false,"pushed_at":"2021-05-29T21:26:24.000Z","size":22,"stargazers_count":39,"open_issues_count":3,"forks_count":10,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-18T11:02:56.166Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/boppreh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-12T02:30:38.000Z","updated_at":"2024-10-02T16:33:32.000Z","dependencies_parsed_at":"2022-08-26T16:30:44.418Z","dependency_job_id":null,"html_url":"https://github.com/boppreh/server-sent-events","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boppreh%2Fserver-sent-events","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boppreh%2Fserver-sent-events/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boppreh%2Fserver-sent-events/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boppreh%2Fserver-sent-events/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/boppreh","download_url":"https://codeload.github.com/boppreh/server-sent-events/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244951375,"owners_count":20537372,"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-10-13T20:37:26.137Z","updated_at":"2025-10-03T13:45:03.211Z","avatar_url":"https://github.com/boppreh.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"server-sent-events\n==================\n\nThis small modules implements a `Publisher` class to handle events in the HTTP\nServer-Sent-Events protocol.\n\nIt allows a number of subscribers to get notifications when events happen in\ncertain feed channels. The common use case is for a Javascript client to\nsubscribe to these feed using an `EventSource` instance, and the events be sent\nby a Python server like `Flask`.\n\nYou can run the module as a Python script to start an example chat, available\nat http://localhost:5000 .\n\n\nFeatures\n========\n\n- Simple `Publisher` with `publish` and `subscribe` methods\n\n- Optional channels support\n\n- Optional subscriber differentiation (subscribers have individual properties\n  that can be used to generate custom events)\n\n- Optional initial data for subscribers\n\n- Server type agnostic\n\n\nAPI Overview\n============\n\n`Publisher()`\n-----------\nCreates a new publisher with an empty list of subscribers. All following\nfunctions are methods of Publisher.\n\n\n`subscribe(channel='default channel', properties=None, initial_data=[])`\n------------------------------------------------------------------------\nSubscribes to the channel(s), returning an infinite generator of\nServer-Sent-Events.\n\nIf `properties` is passed, these will be used for differentiation if a\ncallable object is published (see `Publisher.publish`).\n\nIf the list `initial_data` is passed, all data there will be sent\nbefore the regular channel process starts.\n\n\n`publish(data, channel='default channel')`\n------------------------------------------------\nPublishes data to all subscribers of the given channel(s).\n\nIf data is callable, the return of `data(properties)` will be published\ninstead, for the `properties` object of each subscriber. This allows\nfor customized events.\n\n\n`get_subscribers(channel='default channel')`\n--------------------------------------------------\nReturns a generator of all subscribers in the given channel(s).\n\n\nNote on `channel`\n-----------------\n\n`channel` can either be a channel name (e.g. `'secret room'`) or a list\nof channel names (e.g. `['chat', 'global messages']`). It defaults to\nthe channel named `'default channel'`.\n\n\nExample\n=======\n\nThis is a minimal example using Flask as the server. Every viewer receives a\nnotification when a new viewer visits the page. Open many simultaneous tabs to\nsee the result.\n\nFor simplicity reasons it uses threads to serve the event streams. To build\nactual products you probably want to use gevents/gunicorn.\n\n```Python\nimport flask\nfrom datetime import datetime\nfrom sse import Publisher\n\napp = flask.Flask(__name__)\npublisher = Publisher()\n\n@app.route('/subscribe')\ndef subscribe():\n    return flask.Response(publisher.subscribe(),\n                          content_type='text/event-stream')\n\n@app.route('/')\ndef root():\n    ip = flask.request.remote_addr\n    publisher.publish('New visit from {} at {}!'.format(ip, datetime.now()))\n\n    return \"\"\"\n\u003chtml\u003e\n    \u003cbody\u003e\n        Open this page in new tabs to see the real time visits.\n        \u003cdiv id=\"events\" /\u003e\n        \u003cscript\u003e\n        var eventSource = new EventSource('/subscribe');\n        eventSource.onmessage = function(e) {\n            document.getElementById('events').innerHTML += e.data + '\u003cbr\u003e';\n        }\n        \u003c/script\u003e\n    \u003c/body\u003e\n\u003c/html\u003e\n\"\"\"\n\napp.run(debug=True, threaded=True)\n```\n\nThis example can be run at `sample.py`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboppreh%2Fserver-sent-events","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fboppreh%2Fserver-sent-events","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboppreh%2Fserver-sent-events/lists"}