Skip to content

Rename Client.listen_to_events() to Client.listen() #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ async with client.enter_transaction() as transaction:

Now, let's subscribe to a queue and listen for messages.

Notice that `listen_to_events()` is not bound to a destination: it will listen to all subscribed destinations. If you want separate subscribtions, create separate clients for that.
Notice that `listen()` is not bound to a destination: it will listen to all subscribed destinations. If you want separate subscribtions, create separate clients for that.

```python
async with client.subscribe("DLQ"):
async for event in client.listen_to_events():
async for event in client.listen():
...
```

Expand All @@ -72,7 +72,7 @@ Before learning how to processing messages from server, we need to understand ho
I wanted to avoid them, and came up with an elegant solution: combining async generator and match statement. Here how it looks like:

```python
async for event in client.listen_to_events():
async for event in client.listen():
match event:
case stompman.MessageEvent(body=body):
print(f"message: {body!s}")
Expand All @@ -85,7 +85,7 @@ More complex example, that involves handling all possible events, and auto-ackno

```python
async with asyncio.TaskGroup() as task_group:
async for event in client.listen_to_events():
async for event in client.listen():
match event:
case stompman.MessageEvent(body=body):
task_group.create_task(handle_message(body))
Expand Down Expand Up @@ -132,7 +132,7 @@ stompman takes care of cleaning up resources automatically. When you leave the c
- stompman only runs on Python 3.11 and newer.
- It implements [STOMP 1.2](https://stomp.github.io/stomp-specification-1.2.html) — the latest version of the protocol.
- The client-individual ack mode is used, which means that server requires `ack` or `nack`. In contrast, with `client` ack mode server assumes you don't care about messages that occured before you connected. And, with `auto` ack mode server assumes client successfully received the message.
- Heartbeats are required, and sent automatically on `listen_to_events()` (defaults to 1 second).
- Heartbeats are required, and sent automatically on `listen()` (defaults to 1 second).

Also, I want to pointed out that:

Expand Down
2 changes: 1 addition & 1 deletion stompman/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async def subscribe(self, destination: str) -> AsyncGenerator[None, None]:
finally:
await self._connection.write_frame(UnsubscribeFrame(headers={"id": subscription_id}))

async def listen_to_events(self) -> AsyncIterator[AnyListeningEvent]:
async def listen(self) -> AsyncIterator[AnyListeningEvent]:
async for frame in self._connection.read_frames():
match frame:
case MessageFrame():
Expand Down
2 changes: 1 addition & 1 deletion testing/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async def main() -> None:
stompman.Client(servers=[stompman.ConnectionParameters("0.0.0.0", 61616, "admin", "admin")]) as client, # noqa: S104
client.subscribe("DLQ"),
):
async for event in client.listen_to_events():
async for event in client.listen():
print(event) # noqa: T201
match event:
case stompman.MessageEvent():
Expand Down
2 changes: 1 addition & 1 deletion tests/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def consume() -> None:
received_messages = []

async with asyncio.timeout(5), consumer.subscribe(destination=destination):
async for event in consumer.listen_to_events():
async for event in consumer.listen():
match event:
case stompman.MessageEvent(body=body):
await event.ack()
Expand Down
6 changes: 3 additions & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ async def test_client_listen_to_events_ok() -> None:
)
)
async with EnrichedClient(connection_class=connection_class) as client:
events = [event async for event in client.listen_to_events()]
events = [event async for event in client.listen()]

assert events == [
MessageEvent(_client=client, _frame=message_frame),
Expand All @@ -320,7 +320,7 @@ async def test_client_listen_to_events_unreachable(frame: ConnectedFrame | Recei

async with EnrichedClient(connection_class=connection_class) as client:
with pytest.raises(AssertionError, match="unreachable"):
[event async for event in client.listen_to_events()]
[event async for event in client.listen()]


async def test_ack_nack() -> None:
Expand All @@ -335,7 +335,7 @@ async def test_ack_nack() -> None:

connection_class, collected_frames = create_spying_connection(get_read_frames_with_lifespan([[message_frame]]))
async with EnrichedClient(connection_class=connection_class) as client:
events = [event async for event in client.listen_to_events()]
events = [event async for event in client.listen()]

assert len(events) == 1
event = events[0]
Expand Down