Skip to content

Commit 71f5205

Browse files
committed
Add some initial documentation for WebsocketProviderV2
1 parent 9463136 commit 71f5205

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

docs/providers.rst

+74-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ See:
4848

4949
- :class:`~web3.providers.ipc.IPCProvider`
5050
- :class:`~web3.providers.websocket.WebsocketProvider`
51+
- :class:`~web3.providers.websocket.WebsocketProviderV2`
5152
- :class:`~web3.providers.rpc.HTTPProvider`
5253
- :class:`~web3.providers.async_rpc.AsyncHTTPProvider`
5354

@@ -179,6 +180,12 @@ IPCProvider
179180
WebsocketProvider
180181
~~~~~~~~~~~~~~~~~
181182

183+
.. note::
184+
185+
``WebsocketProviderV2`` is currently in beta and our goal is to fully replace
186+
``WebsocketProvider`` with ``WebsocketProviderV2`` in the next major release
187+
of web3.py.
188+
182189
.. py:class:: web3.providers.websocket.WebsocketProvider(endpoint_uri[, websocket_timeout, websocket_kwargs])
183190
184191
This provider handles interactions with an WS or WSS based JSON-RPC server.
@@ -200,7 +207,7 @@ WebsocketProvider
200207
use the ``websocket_kwargs`` to do so. See the `websockets documentation`_ for
201208
available arguments.
202209

203-
.. _`websockets documentation`: https://websockets.readthedocs.io/en/stable/reference/client.html#websockets.client.WebSocketClientProtocol
210+
.. _`websockets documentation`: https://websockets.readthedocs.io/en/stable/reference/asyncio/client.html#websockets.client.WebSocketClientProtocol
204211

205212
Unlike HTTP connections, the timeout for WS connections is controlled by a
206213
separate ``websocket_timeout`` argument, as shown below.
@@ -211,6 +218,72 @@ WebsocketProvider
211218
>>> from web3 import Web3
212219
>>> w3 = Web3(Web3.WebsocketProvider("ws://127.0.0.1:8546", websocket_timeout=60))
213220
221+
222+
WebsocketProviderV2 (beta)
223+
~~~~~~~~~~~~~~~~~~~~~~~~~~
224+
225+
.. py:class:: web3.providers.websocket.WebsocketProviderV2(endpoint_uri, websocket_kwargs, call_timeout)
226+
227+
This provider handles interactions with an WS or WSS based JSON-RPC server.
228+
229+
* ``endpoint_uri`` should be the full URI to the RPC endpoint such as
230+
``'ws://localhost:8546'``.
231+
* ``websocket_kwargs`` this should be a dictionary of keyword arguments which
232+
will be passed onto the ws/wss websocket connection.
233+
* ``call_timeout`` is the timeout in seconds, used when receiving or sending data
234+
over the connection. Defaults to ``None`` (no timeout).
235+
236+
.. code-block:: python
237+
238+
>>> import asyncio
239+
>>> from web3 import Web3
240+
>>> from web3.providers import WebsocketProviderV2
241+
242+
>>> LOG = True # toggle debug logging
243+
>>> if LOG:
244+
>>> import logging
245+
>>> logger = logging.getLogger("web3.providers.WebsocketProviderV2")
246+
>>> logger.setLevel(logging.DEBUG)
247+
>>> logger.addHandler(logging.StreamHandler())
248+
249+
>>> async def ws_v2_subscription_example():
250+
... async with AsyncWeb3.persistent_websocket(
251+
... WebsocketProviderV2(f"ws://127.0.0.1:8546")
252+
... ) as w3:
253+
... # subscribe to new block headers
254+
... subscription_id = await w3.eth.subscribe("newHeads")
255+
...
256+
... unsubscribed = False
257+
... while not unsubscribed:
258+
... async for response in w3.listen_to_websocket():
259+
... print(f"{response}\n")
260+
... # handle responses here
261+
...
262+
... if some_condition:
263+
... # unsubscribe from new block headers
264+
... unsubscribed = await w3.eth.unsubscribe(subscription_id)
265+
... break
266+
...
267+
... # still an open connection, make any other requests and get
268+
... # responses via send / receive
269+
... latest_block = await w3.eth.get_block("latest")
270+
... print(f"Latest block: {latest_block}")
271+
...
272+
... # the connection closes automatically when exiting the context
273+
... # manager (the `async with` block)
274+
275+
>>> asyncio.run(ws_v2_subscription_example())
276+
277+
278+
Under the hood, the ``WebsocketProviderV2`` uses the python websockets library for
279+
making requests. If you would like to modify how requests are made, you can
280+
use the ``websocket_kwargs`` to do so. See the `websockets documentation`_ for
281+
available arguments.
282+
283+
The timeout for each call to send or receive is controlled by a ``call_timeout``
284+
argument. This is set to ``None`` by default, which means no timeout.
285+
286+
214287
AutoProvider
215288
~~~~~~~~~~~~
216289

docs/web3.providers.websocket.rst

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
web3.providers.websocket package
2+
================================
3+
4+
Submodules
5+
----------
6+
7+
web3.providers.websocket.websocket module
8+
-----------------------------------------
9+
10+
.. automodule:: web3.providers.websocket.websocket
11+
:members:
12+
:undoc-members:
13+
:show-inheritance:
14+
15+
web3.providers.websocket.websocket\_v2 module
16+
---------------------------------------------
17+
18+
.. automodule:: web3.providers.websocket.websocket_v2
19+
:members:
20+
:undoc-members:
21+
:show-inheritance:
22+
23+
Module contents
24+
---------------
25+
26+
.. automodule:: web3.providers.websocket
27+
:members:
28+
:undoc-members:
29+
:show-inheritance:

newsfragments/3048.docs.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add documentation for ``WebsocketProviderV2`` (beta).

0 commit comments

Comments
 (0)