Skip to main content

AsyncSocketPubSub

This module provides Pub/Sub based communication using socket communication.

github: https://github.com/amenaruya/AsyncSocketPubSub

pypi: https://pypi.org/project/AsyncSocketPubSub/

Requirement

This module requires the pubsub module:

pip install pubsub

Usage

install:

pip install AsyncSocketPubSub

Simple using

Use threading for publisher and subscriber when using pubsub module.

from AsyncSocketPubSub.server import PubSubClients
from threading import Thread
import time

# similar to topic in MQTT
CHANNEL = "C"

def Subscriber():
    subClient = PubSubClients(id = "subscriber")
    while True:
        data = subClient.subscribe(channel = CHANNEL)
        print(data)

def Publisher():
    pubClient = PubSubClients(id = "publisher")
    while True:
        pubClient.publish(
            channel = CHANNEL,
            payload = "hello"
        )
        time.sleep(3)

# thread
sub = Thread(
    target = Subscriber,
    daemon = True
)
sub.start()

Publisher()

Server and Client

pubsub module can send/receive the dict objects. This module also can do that as well.

Socket server:

from AsyncSocketPubSub import runServer

runServer(
    host = "0.0.0.0",    # host IP address
    port = 18883         # port number
)

Socket client as publisher:

from socket import socket
from AsyncSocketPubSub.client import PubSubClient

ADDRESS = "127.0.0.1"   # server IP address
PORT = 18883            # server port
ID = "Publisher"        # client ID
CHANNEL = "C"           # channel

# DATA = "hello" # str type
DATA = {"int": 1, "str": "hello", "float": 1.3, "list": ["a", 2]} # dict type

psClient = PubSubClient(
    serverAddress = ADDRESS,
    serverPort = PORT,
    id = ID
)

# connect to server
psClient.connect(socketClient = socket())

# publish
psClient.publish(
    channel = CHANNEL,
    payload = DATA
)

Socket client as subscriber:

from socket import socket
from AsyncSocketPubSub.client import PubSubClient

ADDRESS = "127.0.0.1"   # server IP address
PORT = 18883            # server port
ID = "Subscriber"       # client ID
CHANNEL = "C"           # channel

psClient = PubSubClient(
    serverAddress = ADDRESS,
    serverPort = PORT,
    id = ID
)

# connect to server
psClient.connect(socketClient = socket())

# subscriber
psClient.subscribe(CHANNEL)

Metadata

Release files for AsyncSocketPubSub 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for AsyncSocketPubSub 0.1.1
File Size Uploaded
asyncsocketpubsub-0.1.1.tar.gz 3.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for AsyncSocketPubSub 0.1.1
File Interpreter ABI Platform
AsyncSocketPubSub-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.6 kB

Release files / asyncsocketpubsub-0.1.1.tar.gz

Download URL asyncsocketpubsub-0.1.1.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7c77b7a01380af2460cd861c92c703fcc098898cd05f20fdd83dbd4a01b5791e
BLAKE2b-256 checksum
How to use checksums
0e14f65b3f6f75791de9028322d0d21c7be23c42378ad1fd8a93619253d7d1c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / AsyncSocketPubSub-0.1.1-py3-none-any.whl

Download URL AsyncSocketPubSub-0.1.1-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ec9d71df1ab9439b7e3e37913e13eca49034373be8aa555363eed0794961142e
BLAKE2b-256 checksum
How to use checksums
212f7009dc44bba1d8a9a56f79e129911c82562adbabc1981370ddd185d1a1c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page