Skip to main content

No project description provided

Project description

AsyncSocketPubSub

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

Requirement

This module requires the pubsub module:

pip install pubsub

Usage

directory structure:

┋
┣ AsyncSocketPubSub
┣ xxx.py
┋

see examples

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asyncsocketpubsub-0.1.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

AsyncSocketPubSub-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file asyncsocketpubsub-0.1.0.tar.gz.

File metadata

  • Download URL: asyncsocketpubsub-0.1.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for asyncsocketpubsub-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cf085efd6d20ebf08d3b39d75c65be0ed9dc46f1d05526b7898e6640780cd7b1
MD5 70c537e121532f496acf8bd2b899e5b3
BLAKE2b-256 fe153513288e64ef0b350ee9c8705e2a97450f6e621bd0af9fa96fc01dbfef66

See more details on using hashes here.

File details

Details for the file AsyncSocketPubSub-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for AsyncSocketPubSub-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5721c88be629546b85df6ff9319c1b499a9c7640de901d577f20465193b7840c
MD5 7642c2c8e1b8010fc8a127851463dff0
BLAKE2b-256 24acb36e69d0bb258fb94a99bd7065c371c097efbbfc6bdb449d3977acf672d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page