{"id":16419712,"url":"https://github.com/salaah01/py-pub-sub","last_synced_at":"2026-06-11T01:31:21.447Z","repository":{"id":104266444,"uuid":"473673116","full_name":"Salaah01/py-pub-sub","owner":"Salaah01","description":"A simple pub-sub implementation in Python.","archived":false,"fork":false,"pushed_at":"2022-03-25T12:16:59.000Z","size":13,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-24T13:48:08.454Z","etag":null,"topics":["devops","pubsub","python","python3","server"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Salaah01.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-24T15:51:02.000Z","updated_at":"2025-01-21T06:54:49.000Z","dependencies_parsed_at":"2023-03-13T14:59:30.620Z","dependency_job_id":null,"html_url":"https://github.com/Salaah01/py-pub-sub","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Salaah01/py-pub-sub","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Salaah01%2Fpy-pub-sub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Salaah01%2Fpy-pub-sub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Salaah01%2Fpy-pub-sub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Salaah01%2Fpy-pub-sub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Salaah01","download_url":"https://codeload.github.com/Salaah01/py-pub-sub/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Salaah01%2Fpy-pub-sub/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34178819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-10T02:00:07.152Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["devops","pubsub","python","python3","server"],"created_at":"2024-10-11T07:25:40.433Z","updated_at":"2026-06-11T01:31:21.432Z","avatar_url":"https://github.com/Salaah01.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pub-Sub\nA simple pub-sub implementation in Python.\n\nData will be stored in memory and so will be lost if the program is terminated.\n\nThere are two main apps in this project: server and client.\n\nThe server is a simple TCP server that listens for connections on a port. It has the ability to accept connections and send data to clients.\n\nClient is a simple TCP client that connects to a server and sends data.\n\nThe client is able to subscribe to channels and receive publish messages from the server.\n\n## Sections\n- [Pub-Sub](#pub-sub)\n  - [Sections](#sections)\n  - [Installation](#installation)\n  - [Setup](#setup)\n  - [Running the Server](#running-the-server)\n  - [Using the Client](#using-the-client)\n  - [Setting Up the Pub-Sub](#setting-up-the-pub-sub)\n    - [Server](#server)\n    - [Client - Listening](#client---listening)\n    - [Client - Publishing](#client---publishing)\n## Installation\nThe application uses Python's standard library only and so there are no dependencies.\n\n## Setup\nReview `config.py` to check if you are happy with the settings. The default settings should be fine in most cases. But you may wish to change the port or change the server address to `0.0.0.0`.\n\n## Running the Server\nTo run the server, run any of the following two commands:\n```python\npython3 -m server\n```\nor \n```python\npython3 server/server.py \n```\n\n## Using the Client\nIn `client/client.py` there is a `Client` class that can be used to communicate with the server.\n\nThe client can be used as a context manager. This is the recommended way to use the client so that it may handle errors and gracefully close the connection.\n\n```python\nwith Client() as client:\n  # Do stuff\n```\n\nBelow is a guide of how you can use the client object.\n\n**Connecting to the server**\n```python\nclient.connect()\n```\n\n**Disconnect from the server.**\n```python\nclient.disconnect()\n```\n\n**Sending messages**\n```python\nmessage = 'My message'\nclient.send_message(message)\n```\n\n**Receiving messages**\nThis will block the current thread until a message is received.\n```python\nclient.receive_message()\n```\n\n**Publishing messages to a channel**\n```python\nchannel = 'my_channel\nmessage  = 'My message'\nclient.publish_message(channel, message)\n```\n\n**Subscribe to a channel**\n```python\nchannel = 'my_channel'\nclient.subscribe(channel)\n```\n\n**Unsubscribing from a channel**\n```python\nchannel = 'my_channel'\nclient.unsubscribe(channel)\n```\n\n**Listening for messages on all subscribed channels.**\nThis is a blocking call and will continue to listen until the process is interrupted.\nThe `client.listen` function takes a function that will be called when a message is received. The function will be called with the message as the only argument.\n\n```python\ndef my_function(message):\n  print(f'message received: {message})\n\nclient.listen(my_function)\n```\n## Setting Up the Pub-Sub\nYou will need three shells to run the server and two instances of the client, one for listening and one for publishing messages.\n\n### Server\nOn one shell, run the server:\n```python\npython3 -m server\n```\n\n### Client - Listening\nWithin your code where you wish to use the client you will need to import the\n`Client` class.\n\n```python\nfrom client.client import Client\n```\n(I know, that's a lot of clients!)\n\nNext, create some function that will be called when a message is received.\nWe will create a modified print statement.\n\n```python\ndef my_print(message):\n    print(f'message received: {message}')\n```\n\nSubscribe to some channels and listen for messages.\n```python\nwith Client() as consumer:\n    consumer.connect()\n    consumer.subscribe('channel1')\n    consumer.subscribe('channel2')\n    consumer.listen(my_print)\n```\nThe consumer will now listen for any new messages in `channel1` and `channel2` and call the `my_print` function when a message is received.\n\n### Client - Publishing\nImport the `Client` class.\n\n```python\nfrom client.client import Client\n```\n\nPublish a message.\n```python\nwith Client() as producer:\n    producer.connect()\n    producer.publish_message('channel1', 'message1')\n```\n\nNow, all clients subscribed to `channel1` will receive the message.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsalaah01%2Fpy-pub-sub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsalaah01%2Fpy-pub-sub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsalaah01%2Fpy-pub-sub/lists"}