def publish_gpx_files(client: pynats.NATSClient, files: List[map_point.GpxFile]): publisher_name = "gpx_files.gpx_file" for i, f in enumerate(files): logger.info("publishing file", extra=logging_utils.wrap_props({ "file_name": f.name, "file_number": i })) payload = nats_payload.Payload(sender="pg_pull_nats_push", data={"file_id": f.id}) payload_bytes = payload.to_payload() publish_name = publisher_name client.publish(subject=publish_name, payload=payload_bytes)
def test_request(nats_plain_url): def worker(): with NATSClient(nats_plain_url, socket_timeout=2) as client: def callback(message): client.publish(message.reply, payload=b"test-callback-payload") client.subscribe("test-subject", callback=callback, queue="test-queue", max_messages=2) client.wait(count=2) t = threading.Thread(target=worker) t.start() time.sleep(1) with NATSClient(nats_plain_url, socket_timeout=2) as client: # request without payload resp = client.request("test-subject") assert resp.subject.startswith("_INBOX.") assert resp.reply == "" assert resp.payload == b"test-callback-payload" # request with payload resp = client.request("test-subject", payload=b"test-payload") assert resp.subject.startswith("_INBOX.") assert resp.reply == "" assert resp.payload == b"test-callback-payload" t.join()
def test_subscribe_unsubscribe(nats_plain_url): with NATSClient(nats_plain_url, socket_timeout=2) as client: sub = client.subscribe("test-subject", callback=lambda x: x, queue="test-queue", max_messages=2) client.unsubscribe(sub)
def sub(que): def push_in_queue(msg): que.put(msg.payload.decode()) with NATSClient(name=name + "sub") as client: client.subscribe(str(room), callback=push_in_queue) client.wait()
def test_subscribe_timeout(nats_url): with NATSClient(nats_url, socket_timeout=2) as client: sub = client.subscribe( "test-subject", callback=lambda x: x, queue="test-queue", max_messages=1 ) with pytest.raises(socket.timeout): client.wait(count=1) client.unsubscribe(sub)
def worker(): with NATSClient(nats_url, socket_timeout=2) as client: def callback(message): received.append(message) client.subscribe( "test-subject", callback=callback, queue="test-queue", max_messages=2 ) client.wait(count=2)
def test_graceful_shutdown(nats_plain_url): def worker(client, connected_event): client.connect() connected_event.set() try: client.wait() except NATSReadSocketError: assert True except Exception: raise AssertionError("unexpected Exception raised") client = NATSClient(nats_plain_url) connected_event = threading.Event() thread = threading.Thread(target=worker, args=[client, connected_event]) thread.start() assert connected_event.wait(5), "unable to connect" client.close() thread.join(5) assert not thread.is_alive(), "thread did not finish"
def worker(): with NATSClient(nats_url, socket_timeout=2) as client: def callback(message): client.publish(message.reply, payload=b"test-callback-payload") client.subscribe( "test-subject", callback=callback, queue="test-queue", max_messages=2 ) client.wait(count=2)
def read_temp(signum, frame): with open(device_file, 'r') as f: temp_s = f.readline() temp = float(temp_s) / 1000.0 cas = time.strftime("%Y-%m-%dT%H:%M:%S") data = {'time': cas, 'data': {'temperature': temp}} payload = dumps(data) try: with NATSClient("nats://rpi3:4222") as client: client.publish("rpi3", payload=payload.encode()) pause() except ConnectionRefusedError: read_temp(14, None)
def add(todo: str = Form(...)): ### ... means todo param is required if len(todo) <= 140: logger.info(f"Adding todo: {todo}") with conn: with conn.cursor() as cur: cur.execute(f"INSERT INTO todos(todo) VALUES ('{todo}')") with NATSClient(os.environ["NATS_URL"]) as client: message = {"msg": todo, "type": "inserted"} client.publish("broadcaster", payload=json.dumps(message)) else: logger.warn(f"Todo message too long, not added") return RedirectResponse("/todos", status_code=status.HTTP_303_SEE_OTHER)
def __init__( self, run_panini: typing.Callable = lambda *args, **kwargs: None, panini_service_name: str = "*", panini_client_id: str = "*", use_web_server: bool = False, use_web_socket: bool = False, base_web_server_url: str = "http://127.0.0.1:8080", base_nats_url: str = "nats://127.0.0.1:4222", socket_timeout: int = 5, name: str = "__".join([ "test_client", str(random.randint(1, 10000000)), str(random.randint(1, 10000000)), ]), ): self.run_panini = run_panini self.panini_service_name = panini_service_name self.panini_client_id = panini_client_id self.base_web_server_url = base_web_server_url self.use_web_server = use_web_server self.use_web_socket = use_web_socket self.nats_client = NATSClient( url=base_nats_url, name=name, socket_timeout=socket_timeout, ) self.nats_client.connect() if use_web_server: self.http_session = HTTPSessionTestClient( base_url=base_web_server_url) if use_web_socket: self.websocket_session = websocket.WebSocket() self.panini_process = None
def update(todo_id: int = Form(...)): try: with conn: with conn.cursor() as cur: cur.execute( f"UPDATE todos SET done='t' WHERE id = {todo_id} RETURNING todo" ) todo = cur.fetchone()[0] with NATSClient(os.environ["NATS_URL"]) as client: message = {"msg": todo, "type": "updated"} client.publish("broadcaster", payload=json.dumps(message)) except Exception as e: print(e) raise HTTPException(status_code=500, detail="Server error") return RedirectResponse("/todos", status_code=status.HTTP_303_SEE_OTHER)
def worker(): with NATSClient(nats_plain_url, socket_timeout=2) as client: def callback(message): client.publish( message.reply, payload=msgpack.packb( {b"v": 3338} if message.payload else {b"v": 32}), ) client.subscribe("test-subject", callback=callback, queue="test-queue", max_messages=2) client.wait(count=2)
def test_publish(nats_plain_url): received = [] def worker(): with NATSClient(nats_plain_url, socket_timeout=2) as client: def callback(message): received.append(message) client.subscribe("test-subject", callback=callback, queue="test-queue", max_messages=2) client.wait(count=2) t = threading.Thread(target=worker) t.start() time.sleep(1) with NATSClient(nats_plain_url, socket_timeout=2) as client: # publish without payload client.publish("test-subject") # publish with payload client.publish("test-subject", payload=b"test-payload") t.join() assert len(received) == 2 assert received[0].subject == "test-subject" assert received[0].reply == "" assert received[0].payload == b"" assert received[1].subject == "test-subject" assert received[1].reply == "" assert received[1].payload == b"test-payload"
def test_request_msgpack(nats_plain_url): def worker(): with NATSClient(nats_plain_url, socket_timeout=2) as client: def callback(message): client.publish( message.reply, payload=msgpack.packb( {b"v": 3338} if message.payload else {b"v": 32}), ) client.subscribe("test-subject", callback=callback, queue="test-queue", max_messages=2) client.wait(count=2) t = threading.Thread(target=worker) t.start() time.sleep(1) with NATSClient(nats_plain_url, socket_timeout=2) as client: # request without payload resp = client.request("test-subject") assert resp.subject.startswith("_INBOX.") assert resp.reply == "" assert msgpack.unpackb(resp.payload) == {b"v": 32} # request with payload resp = client.request("test-subject", payload=msgpack.packb("test-payload")) assert resp.subject.startswith("_INBOX.") assert resp.reply == "" assert msgpack.unpackb(resp.payload) == {b"v": 3338} t.join()
def send_message(data, channel=None): if channel is None: channel = settings.NATS_CHANNEL_OUT status = False # nc = SyncClient(settings.NATS_HOST_PORT) nc = SyncClient(f'nats://{settings.NATS_HOST_PORT}') try: nc.connect() data_enc = dumps(data).encode('utf-8') response_data = {} try: # payload = json.dumps(data_enc) nc.publish(channel, payload=data_enc) # response_data = json.loads(_response.payload) except ErrTimeout: print("Request timed out") # response = response_data.get('subject', '') status = response_data.get('received', False) status = True except Exception as e: print(f'{str(e)}') nc.close() return status
here = Path(__file__).absolute().parent csv_file = here / 'taxi.csv.bz2' conversions = [ ('VendorID', 'vendor', int), ('tpep_pickup_datetime', 'pickup', str), ('tpep_dropoff_datetime', 'dropoff', str), ('passenger_count', 'passengers', int), ('trip_distance', 'distance', float), ('tip_amount', 'tip', float), ('total_amount', 'amount', float), ] def iter_rides(): with bz2.open(csv_file, 'rt') as fp: for row in csv.DictReader(fp): record = {} for src, dest, conv in conversions: record[dest] = conv(row[src]) yield record client = NATSClient('nats://localhost:4222') client.connect() for ride in iter_rides(): sleep(random()) payload = json.dumps(ride) client.publish('rides', payload=payload)
def test_invalid_scheme(): client = NATSClient("http://127.0.0.1:4224") with pytest.raises(NATSInvalidSchemeError): client.connect()
def test_tls_connect(nats_tls_url): client = NATSClient(nats_tls_url, socket_timeout=2) client.connect() client.ping() client.close()
def test_reconnect(nats_plain_url): client = NATSClient(nats_plain_url, socket_timeout=2) client.connect() client.ping() client.reconnect() client.ping() client.close()
def test_connect_timeout(): client = NATSClient("nats://127.0.0.1:4223", socket_timeout=2) with pytest.raises(socket.error): client.connect()
def test_connect_and_close_using_context_manager(nats_plain_url): with NATSClient(nats_plain_url, socket_timeout=2) as client: client.ping()
def nats_publish(nats_url, msg, topic): # TODO fix the settings reading msg_to_send = msg.SerializeToString() with NATSClient(nats_url) as client: client.publish(topic, payload=msg_to_send)
def test_request_timeout(nats_plain_url): with NATSClient(nats_plain_url, socket_timeout=2) as client: with pytest.raises(socket.timeout): client.request("test-subject")
#!/usr/bin/python3 from pynats import NATSClient import argparse import json subject = 'message/sensor/92868a' msg = { "device_code": "92868a-vr88", "date_add": "2020-06-07 10:10:10", "gyro": { "x": 11.1, "y": 12.4, "z": 15 }, "proximity": 111.5, "rssi": 10.5 } msg = json.dumps(msg) if __name__ == '__main__': client = NATSClient("nats://103.56.148.215:4222", socket_timeout=2, verbose=True) client.connect() #client.ping() client.publish(subject, payload=msg) client.close()
from pynats import NATSClient import argparse import json from datetime import datetime broker = "127.0.0.1" #"161.117.58.227" port = "4222" subject = 'message/sensor/xzhu2l' client = NATSClient("nats://" + broker + ":" + port, socket_timeout=2, verbose=True) client.connect() today = datetime.today() #current-datetime msg = { "device_code": "xzhu2l-cq91", "date_add": round(datetime.today().timestamp() * 1000), #today.strftime("%Y-%m-%d %H:%M:%S"), "gps": { "latitude": -7.575973, "longitude": 112.878304 }, "temperature": 25.5, "fuel": 1000 } payload = json.dumps(msg) client.publish(subject, payload=payload) client.close()
def generate_blocking_client(host=DEFAULT_HOST, port=DEFAULT_PORT): client = BlockingNATSClient('nats://{}:{}'.format(host, port)) client.connect() return client
#!/usr/bin/python3 from pynats import NATSClient import argparse import json import sys, json, time broker_address = "103.56.148.215" port = "4222" client = NATSClient("nats://"+broker_address+":"+port,verbose=True) def publish(topic,message): try: print(topic) print(message) sys.stdout.flush() client.connect() client.publish(topic, payload=json.dumps(message)) client.close() except: print("failed") return
here = Path(__file__).absolute().parent csv_file = here / os.path.join(THIS_FOLDER, "taxi.csv.bz2") conversions = [ ("VendorID", "vendor", int), ("tpep_pickup_datetime", "pickup", str), ("tpep_dropoff_datetime", "dropoff", str), ("passenger_count", "passengers", int), ("trip_distance", "distance", float), ("tip_amount", "tip", float), ("total_amount", "amount", float), ] def iter_rides(): with bz2.open(csv_file, "rt") as fp: for row in csv.DictReader(fp): record = {} for src, dest, conv in conversions: record[dest] = conv(row[src]) yield record client = NATSClient("nats://localhost:4222") client.connect() for ride in iter_rides(): sleep(random()) payload = json.dumps(ride) client.publish("rides", payload=payload)
client.subscribe(str(room), callback=push_in_queue) client.wait() ps = Process(target=sub, args=(in_queue, )) ps.start() layout = [[sg.Output((100, 20), key='out')], [sg.Text('mesg'), sg.InputText(key='msg')], [sg.Ok()]] window = sg.Window('room' + str(room) + ':' + str(name), layout) while True: event, values = window.read(100) if event in (sg.WIN_CLOSED, 'Cancel'): break if event == 'Ok': txt = values.get('msg', None) if txt is not None and len(txt) > 0: with NATSClient(name=name + "pub") as client: client.publish(str(room), payload=str(name + ": " + txt).encode()) try: t = in_queue.get(block=False) print(t) except Exception: pass window.close() ps.kill() ps.join()