sseclient icon indicating copy to clipboard operation
sseclient copied to clipboard

Too big events

Open Michdo93 opened this issue 2 years ago • 1 comments

Hi,

I use SSE to query the events of items from openHAB. I can do this there via the REST API. However, I have a problem with images. If I change a large image and replace it with a small one, the SSE events do not break.

However, if I replace a small image with a large one, it fails tremendously.

Here the part of my code:

import json
import pprint
import sseclient

if __name__ == "__main__":
    events = sseclient.SSEClient("http://192.168.0.5:8080/rest/events?topics=openhab/items/testImage/state")

    for event in events:
        print(event)
        print(type(event))
        print(type(event.data))
        print(len(event.data))
        pprint.pprint(event)

        try:
            if len(event.data) > 0:
                data = json.loads(event.data)

                event_topic = data.get("topic")
                event_item_name = event_topic.split("/")[2]
                event_payload = eval(data.get("payload"))
                event_type = data.get("type")

                print(event_item_name)
        except json.decoder.JSONDecodeError:
            print("Event could not be converted from JSON")

I check it in return with Curl, of course. There I see that a big event is arriving.

If I run my Python script, I get:

{"topic":"openhab/items/testImage/state","payload":"{\"type\":\"Raw\",\"value\":\"data:image/jpg;base64,R0lGODlhAQABAIAAAP7//wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw\\u003d\\u003d\"}","type":"ItemStateEvent"}
<class 'sseclient.Event'>
<class 'str'>
205
<sseclient.Event object at 0x7f435da7ffa0>
testImage

<class 'sseclient.Event'>
<class 'str'>
0
<sseclient.Event object at 0x7f435da7ff70>

<class 'sseclient.Event'>
<class 'str'>
0
<sseclient.Event object at 0x7f435da7ffa0>

<class 'sseclient.Event'>
<class 'str'>
0
...

Or in other words: Instead of receiving anything big, I suddenly receive nothing at all. The loop makes another iteration. That's all well and good. But for some reason this event is then empty. My assumption would be that it would be too large to be processed.

This is the equvialent command with curl:

curl "http://192.168.0.5:8080/rest/events?topics=openhab/items/testImage/state"

I receive:

...
event: message
data: {"topic":"openhab/items/testImage/state","payload":"{\"type\":\"Raw\",\"value\":\"data:image/jpg;base64,
....

It would be definitely too long to post the whole answer because an image is very long.

Michdo93 avatar Sep 16 '22 11:09 Michdo93

I've definitely never validated this library on large events when all the data is on a single line.

Can you try enabling debug logging on the library's logger? I'm trying to see if it's hitting the check from https://github.com/mpetazzoni/sseclient/blob/master/sseclient/init.py#L75

mpetazzoni avatar Sep 19 '22 14:09 mpetazzoni