|
| 1 | +--- |
| 2 | +slug: /guides/rsocket-py/client |
| 3 | +title: RSocketClient - rsocket-py |
| 4 | +sidebar_label: Introduction |
| 5 | +--- |
| 6 | + |
| 7 | +An `rsocket-py` client can be used to communicate with any RSocket Server implemented against the same protocol version as the client, |
| 8 | +and which implements the same transport as the client. |
| 9 | + |
| 10 | +Available network transports for `rsocket-py` client include: |
| 11 | + |
| 12 | +- TCP - available by default |
| 13 | +- Websocket (aiohttp) |
| 14 | + |
| 15 | +The `RSocketClient` class should be passed an instance of one of the available transports. |
| 16 | + |
| 17 | +To get started creating an RSocket client, you will need to install the [rsocket](https://pypi.org/project/rsocket/) package, |
| 18 | +and at least one transport protocol implementation (TCP available by default). |
| 19 | + |
| 20 | +### Client Quick Start Example |
| 21 | + |
| 22 | +``` |
| 23 | +npm install rsocket-core rsocket-websocket-client |
| 24 | +``` |
| 25 | + |
| 26 | +```py |
| 27 | +import asyncio |
| 28 | +from rsocket.rsocket_client import RSocketClient |
| 29 | +from rsocket.transports.tcp import TransportTCP |
| 30 | + |
| 31 | +async def main(): |
| 32 | + connection = await asyncio.open_connection('localhost', 6565) |
| 33 | + |
| 34 | + async with RSocketClient(TransportTCP(*connection)) as client: |
| 35 | + ... # Execute requests |
| 36 | + |
| 37 | +if __name__ == '__main__': |
| 38 | + asyncio.run(main()) |
| 39 | +``` |
| 40 | + |
| 41 | +## Client API |
| 42 | + |
| 43 | +The `rsocket-py` package exposes the following types: |
| 44 | + |
| 45 | +## RSocketClient (class) |
| 46 | + |
| 47 | +`RSocketClient` is used to create an instance of a client. The clients' connection does not initialize until |
| 48 | +the `connect` method is invoked, or it is used as a context-manager. |
| 49 | + |
| 50 | +### constructor (function) |
| 51 | + |
| 52 | + |
| 53 | +#### serializers (property) |
| 54 | + |
| 55 | +The `data` and `metadata` of each payload are passed through to the |
| 56 | +transport layer as-is. This is appropriate for sending/receiving strings/binary. |
| 57 | + |
| 58 | + |
| 59 | +#### transport (property) |
| 60 | + |
| 61 | +This will typically be an instance conforming to the API of the `Transport` class. |
| 62 | + |
| 63 | + |
| 64 | +### connect() (method) |
| 65 | + |
| 66 | +This method opens the connection to the peer. Internally this calls `connect()` on the |
| 67 | +transport client. See below for the `RSocket` interface. |
| 68 | + |
| 69 | +## RSocket (interface) |
| 70 | + |
| 71 | +This interface represents an instance of a rsocket peer-to-peer connection, providing the five |
| 72 | +core interactions (fire/forget, request/response, etc.): |
| 73 | + |
| 74 | +### fire_and_forget() (method) |
| 75 | + |
| 76 | +This method sends data/metadata to the server without waiting for a response. The data is |
| 77 | +sent immediately. |
| 78 | + |
| 79 | +```py |
| 80 | +from rsocket.payload import Payload |
| 81 | + |
| 82 | +def fire_and_forget(self, payload: Payload): |
| 83 | + ... |
| 84 | +``` |
| 85 | + |
| 86 | +### request_response() (method) |
| 87 | + |
| 88 | +This method sends data/metadata to the server, which returns a single response. The data is |
| 89 | +sent lazily when the returned `Future` is resolved. |
| 90 | + |
| 91 | +```py |
| 92 | +from rsocket.payload import Payload |
| 93 | +from asyncio import Future |
| 94 | + |
| 95 | +def request_response(self, payload: Payload) -> Future: |
| 96 | + ... |
| 97 | +``` |
| 98 | + |
| 99 | +### request_stream() (method) |
| 100 | + |
| 101 | +This method sends data/metadata to the server, which returns a stream of responses. The semantics |
| 102 | +of the stream are application-specific. For example, the stream may represent |
| 103 | +updates to a single conceptual value over time, items in an incrementally loaded |
| 104 | +list, events, etc. The data is sent to the peer lazily when the returned |
| 105 | +`Publisher` is subscribed to and `request(n)` is called to signal demand. |
| 106 | + |
| 107 | +```py |
| 108 | +from typing import Union |
| 109 | + |
| 110 | +from reactivestreams.publisher import Publisher |
| 111 | +from rsocket.payload import Payload |
| 112 | +from rsocket.streams.stream import Stream |
| 113 | + |
| 114 | +def request_stream(self, payload: Payload) -> Union[Stream, Publisher]: |
| 115 | + ... |
| 116 | +``` |
| 117 | + |
| 118 | +### requestChannel() (method) |
| 119 | + |
| 120 | +This method establishes an understanding between a client and a server where each intends to send and receive streams |
| 121 | +of data from the other. Each actor in this relationship is responsible for signaling to the other that they are ready |
| 122 | +to receive data by invoking `request(n)`, where `n` is the max number of payloads the actor is comfortable handling. |
| 123 | +Conceptually, `request_channel` can be thought of as two entities 'polling' from each other by signaling to the others |
| 124 | +that they are ready to accept `n` number of messages. Inversely, `request_channel` can be leveraged to facilitate |
| 125 | +a consistent stream of data transfer payloads between client and server by each (or either) |
| 126 | +invoking `request(0x7fffffff)`, where `0x7fffffff` is the max integer value for `int32`. |
| 127 | + |
| 128 | +```py |
| 129 | +from typing import Union, Optional |
| 130 | + |
| 131 | +from reactivestreams.publisher import Publisher |
| 132 | +from rsocket.payload import Payload |
| 133 | +from rsocket.streams.stream import Stream |
| 134 | + |
| 135 | +def request_channel(self, payload: Payload, publisher: Optional[Publisher] = None) -> Union[Stream, Publisher]: |
| 136 | + ... |
| 137 | +``` |
| 138 | + |
| 139 | +### metadata_push() (method) |
| 140 | + |
| 141 | +This method sends metadata only to the server without waiting for a response. The payload is |
| 142 | +sent immediately. This method is not for the direct application usage and should be used to exchange some service level information |
| 143 | + |
| 144 | +```python |
| 145 | +def metadata_push(self, metadata: bytes): |
| 146 | + ... |
| 147 | +``` |
0 commit comments