Skip to content

Recieve messages failed: {'information': '', 'exception': ReadError('')} #212

Open
@MtkN1

Description

@MtkN1

I added pubnub.publish to the main function in examples/pubnub_asyncio_simple/main.py. After that, pressing Ctrl+C results in the following error output (ReadError). What could be the issue?

Python 3.13

import asyncio
import os

from pubnub.enums import PNStatusCategory
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub_asyncio import PubNubAsyncio, SubscribeCallback


class MySubscribeCallback(SubscribeCallback):
    def status(self, pubnub, status):
        if status.category == PNStatusCategory.PNUnexpectedDisconnectCategory:
            print("Disconnected")
        elif status.category == PNStatusCategory.PNConnectedCategory:
            print("Connected")
        elif status.category == PNStatusCategory.PNReconnectedCategory:
            print("Reconnected")
        elif status.category == PNStatusCategory.PNDecryptionErrorCategory:
            print("Decryption error")

    def message(self, pubnub, message):
        print(f"Received message: {message.message} on channel: {message.channel}")

    def presence(self, pubnub, presence):
        print(f"Presence event: {presence.event}")


async def main(pubnub):
    pubnub.subscribe().channels("my_channel").execute()

    # Added
    await asyncio.sleep(1)
    await pubnub.publish(
        channel="my_channel", message="Hello from PubNub Python SDK"
    ).future()

    print("Listening for messages...")
    while True:
        await asyncio.sleep(1)


if __name__ == "__main__":
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    pnconfig = PNConfiguration()
    pnconfig.subscribe_key = os.getenv("PUBNUB_SUBSCRIBE_KEY") or "demo"
    pnconfig.publish_key = os.getenv("PUBNUB_PUBLISH_KEY") or "demo"
    pnconfig.user_id = "my_unique_user_id"  # Set a unique user ID

    pubnub = PubNubAsyncio(pnconfig)
    callback = MySubscribeCallback()
    pubnub.add_listener(callback)

    try:
        loop.run_until_complete(main(pubnub))
    except KeyboardInterrupt:
        print("Interrupted by user. Exiting...")
    finally:
        loop.run_until_complete(pubnub.stop())  # Assuming 'pubnub' is in scope
        loop.close()
Connected
Listening for messages...
Received message: Hello from PubNub Python SDK on channel: my_channel
^CInterrupted by user. Exiting...
session.request exception: 
Recieve messages failed: {'information': '', 'exception': ReadError('')}
EventEngine is not enabled

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions