Skip to main content

No project description provided

Project description

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)

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.1.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.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: asyncsocketpubsub-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 7c77b7a01380af2460cd861c92c703fcc098898cd05f20fdd83dbd4a01b5791e
MD5 125c5e96df6d2bfebba6cd7af6ec7b0e
BLAKE2b-256 0e14f65b3f6f75791de9028322d0d21c7be23c42378ad1fd8a93619253d7d1c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for AsyncSocketPubSub-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ec9d71df1ab9439b7e3e37913e13eca49034373be8aa555363eed0794961142e
MD5 fb0c9a6007e0010aed756e97343ffad1
BLAKE2b-256 212f7009dc44bba1d8a9a56f79e129911c82562adbabc1981370ddd185d1a1c5

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