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
179180WebsocketProvider
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+
214287AutoProvider
215288~~~~~~~~~~~~
216289
0 commit comments