Giter Club home page Giter Club logo

Comments (2)

takanoshota avatar takanoshota commented on May 27, 2024

lt' probably due to Delimiters of nats protocol(https://github.com/nats-io/docs/blob/master/nats_protocol/nats-protocol.md).

Codes below works for me.

note

If you want to send over 1MB message, you need to change default value of
nats server by editing nats-server.conf like as max_payload: 100000000(it means 100MB).

publisher.py

import cv2
import asyncio
from nats.aio.client import Client as NATS
import base64

async def run(loop):
    nc = NATS()
    await nc.connect(loop=loop)
    cap = cv2.VideoCapture(0)
    while True:
        frame = cap.read()[1]
        frame = cv2.resize(frame, dsize=(640,360))
        cv2.imwrite('before.png',frame)
        frame_base64 = base64.b64encode(frame)
        await nc.publish('foo', frame_base64)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(loop))
    loop.run_forever()

subscriber.py

import asyncio
from nats.aio.client import Client as NATS
import base64
import numpy as np
import cv2

async def run(loop):
    nc = NATS()
    await nc.connect(loop=loop)

    async def receiver(msg):
        data = msg.data.decode()
        frame_encoded = base64.b64decode(data)
        frame_encoded = np.frombuffer(frame_encoded, dtype=np.uint8)
        frame_encoded =frame_encoded.reshape(360,640,3)
        await cv2.imwrite('after.png',frame_encoded)

    await nc.subscribe("foo", cb=receiver)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(loop))
    loop.run_forever()

from nats.py.

wallyqs avatar wallyqs commented on May 27, 2024

This use case would now be better suited with OrderedConsumers from JetStream: https://github.com/nats-io/nats.py/blob/a1a9270/tests/test_js.py#L523-L590

from nats.py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.