PSRT library methods

https://github.com/alttch/psrt-py

Client usage example:

from psrt import Client
import time

def process_message(client, userdata, message):
    print(message.topic)
    print(message.payload)


client = Client(path='localhost:2873')
# client.tls = True
# client.tls_ca = '/opt/workstuff/psrt/certs/ca.crt'
# client.need_data_socket = False
client.on_message = process_message
client.connect()
client.subscribe('test')
client.subscribe_bulk(['test2', '#'])
client.unsubscribe('test')
client.unsubscribe_bulk(['test2', '#'])
client.subscribe('#')
for _ in range(3):
    client.publish('unit/tests/test1', 'hello')
    time.sleep(1)
client.bye()
exception psrt.AccessError
class psrt.Client(**kwargs)

PSRT client

Initialize PSRT client

Additioanal properties which can be set directly to client object:

  • on_message = on_message(client, userdata, message) # message handler

  • on_connect(self, client, userdata, flags, rc) # connect handler

(as the connection is performed in the current thread, on_connect is used for paho-mqtt compat. only)

Optional:
  • path: host:port or (host, port) tuple

  • user: user name

  • password: password

  • timeout: client timeout

  • buf_size: socket and message buffer (set 100K+ for large frames)

  • userdata: anything useful

  • tls: use TLS (default: False)

  • tls_ca: path to an alternative CA file

bye()

End communcation

connect(host=None, port=2873, keepalive=None)

Connect the client

Optional:
  • host: ovverride server host

  • port: override server port

  • keepalive: not used, for paho-mqtt compat-only

connect_cluster(paths, randomize=True)

Connect the client to PSRT cluster

If randomize parameter is set to False, the nodes are chosen in the listed order

Parameters

paths – list of node paths (host:port or tuples)

Optional:
  • randomize: choose random node (default: True)

Returns

Successful node path if connected

Raises

RuntimeError – if no nodes available

is_connected()

Check is the client connected

publish(topic, message, qos=None, retain=None)

Publish a message

Parameters
  • topic – topic name

  • message – message (string, bytes or anyting which can be str())

Optional:
  • qos: not used, for paho-mqtt compat-only

  • retain: not used, for paho-mqtt compat-only

subscribe(topic, qos=None)

Subscribe to a topic

Parameters

topic – topic name

Optional:
  • qos: not used, for paho-mqtt compat-only

subscribe_bulk(topics)

Subscribe to topics

Parameters

topics – topic names (list or tuple)

Optional:
  • qos: not used, for paho-mqtt compat-only

unsubscribe(topic)

Unsubscribe from a topic

Parameters

topic – topic name

Optional:
  • qos: not used, for paho-mqtt compat-only

unsubscribe_bulk(topics)

Unsubscribe from topics

Parameters

topics – topic names (list or tuple)

Optional:
  • qos: not used, for paho-mqtt compat-only

psrt.pub_udp(target, topic, message, need_ack=True, check_ack_src=True, **kwargs)

Publish message with UDP frame

Parameters
  • target – host:port or (host, port) tuple

  • topic – topic to publish

  • message – message (string, bytes or anyting which can be str())

Optional:
  • need_ack: require server acknowledge (default: True)

  • check_ack_src: check acknowledge source (host/port, default: True)

  • user: user name

  • password: password

  • timeout: socket timeout