You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: Improve the documentation page with Response class (#2238)
* Improve docs
* docs: generate API References
* Add information about `NatsResponse` class
* docs: generate API References
* Correct response headers, revert example structure
* Added documentation for other brokers
* docs: generate API References
* docs: polish style and text
---------
Co-authored-by: Nikita Pastukhov <[email protected]>
@@ -38,7 +35,7 @@ Also, if you want to create a permanent request-reply data flow, probably, you s
38
35
39
36
So, if you have such one, you can specify it with the `reply_to` argument. This way, **FastStream** will send a response to this subject automatically.
40
37
41
-
```python hl_lines="1 8"
38
+
```pythonlinenums="1" hl_lines="1 8"
42
39
@broker.subscriber("response-subject")
43
40
asyncdefconsume_responses(msg):
44
41
...
@@ -49,3 +46,89 @@ await broker.publish(
49
46
reply_to="response-subject",
50
47
)
51
48
```
49
+
50
+
## Creating an RPC subscriber
51
+
52
+
To handle an RPC request, you need to create a subscriber that processes the incoming message and returns a response.
53
+
The subscriber should be decorated with `#!python @broker.subscriber` and return either a raw value or a `Response` object.
54
+
55
+
Below is an example of a simple RPC subscriber that processes a message and returns a response.
For NATS-specific use cases, you can use the `NatsResponse` class instead of the generic `Response` class.
118
+
119
+
The `NatsResponse` class extends `Response` and adds support for specifying a `stream` parameter. This ensures the response is published to the correct stream in a JetStream context.
120
+
121
+
```python linenums="1" hl_lines="1 7-13"
122
+
from faststream.nats import NatsBroker, NatsResponse
For RabbitMQ-specific use cases, you can use the `RabbitResponse` class instead of the generic `Response` class.
115
+
116
+
The `RabbitResponse` class extends `Response` and adds support for RabbitMQ-specific message properties, such as `message_id`, `priority`, `expiration`, and more.
117
+
118
+
This is particularly useful when you need fine-grained control over the message properties in a RabbitMQ context.
119
+
120
+
Below is an example of how to use the `RabbitResponse` class in an RPC subscriber.
121
+
122
+
```python linenums="1" hl_lines="1 7-14"
123
+
from faststream.rabbit import RabbitBroker, RabbitResponse
Copy file name to clipboardExpand all lines: docs/docs/en/redis/rpc.md
+94Lines changed: 94 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,3 +56,97 @@ Combining all the code snippets above, here is the complete example of how to se
56
56
```
57
57
58
58
By embracing **Redis**RPCwith**FastStream**, you can build sophisticated message-based architectures that require direct feedback from message processors. This feature is particularly suitable for cases where immediate processing is necessary or calling functions across different services is essential.
59
+
60
+
## Creating an RPC subscriber
61
+
62
+
To handle an RPC request, you need to create a subscriber that processes the incoming message and returns a response.
63
+
The subscriber should be decorated with`#!python @broker.subscriber` and return either a raw value or a `Response` object.
64
+
65
+
Below is an example of a simple RPC subscriber that processes a message and returns a response.
The subscriber processes the request and sends back the response, which is received by the client.
90
+
91
+
!!! tip
92
+
You can use the `no_reply=True` flag in the `#!python @broker.subscriber` decorator to suppress automatic RPC and `reply_to` responses.
93
+
This is useful when you want the subscriber to process the message without sending a response back to the client.
94
+
95
+
## Using the Response class
96
+
97
+
The `Response`class allows you to attach metadata, such as headers, to the response message.
98
+
This is useful for adding context or tracking information to your responses.
99
+
100
+
Below is an example of how to use the `Response`classin an RPC subscriber.
101
+
102
+
```python linenums="1" hl_lines="1 8-12"
103
+
from faststream import Response
104
+
from faststream.redis import RedisBroker
105
+
106
+
broker = RedisBroker()
107
+
108
+
@broker.subscriber(channel="test-channel")
109
+
asyncdef handle(msg):
110
+
return Response(
111
+
body=f"Processed: {msg}",
112
+
headers={"x-token": "some-token"},
113
+
correlation_id="some-correlation-id",
114
+
)
115
+
```
116
+
117
+
When the client sends a request:
118
+
119
+
```python linenums="1" hl_lines="7-9"
120
+
from faststream.redis import RedisMessage
121
+
122
+
msg: RedisMessage = await broker.request(
123
+
"Hello, Redis!",
124
+
channel="test-channel",
125
+
)
126
+
assert msg.body ==b"Processed: Hello, Redis!"
127
+
assert msg.headers == {"x-token": "some-token"}
128
+
assert msg.correlation_id =="some-correlation-id"
129
+
```
130
+
131
+
## Using the RedisResponse class
132
+
133
+
For Redis-specific use cases, you can use the `RedisResponse`class instead of the generic `Response`class.
134
+
135
+
The `RedisResponse`class extends `Response`and adds support for specifying a `maxlen` parameter, which is useful when publishing responses to a Redis stream to limit the stream's length. This option could be helpful with Reply-To feature, when reply-to destination is a Redis stream.
136
+
137
+
Below is an example of how to use the RedisResponse classin an RPC subscriber.
138
+
139
+
```python linenums="1" hl_lines="1 7-12"
140
+
from faststream.redis import RedisBroker, RedisResponse
0 commit comments