1212 * [ Client] ( #client )
1313 * [ authenticate] ( #authenticate )
1414 * [ close] ( #close )
15+ * [ close_and_wait] ( #close_and_wait )
1516 * [ connect] ( #connect )
1617 * [ connect_pool] ( #connect_pool )
1718 * [ get_default_scope] ( #get_default_scope )
1819 * [ get_event_loop] ( #get_event_loop )
1920 * [ get_rooms] ( #get_rooms )
2021 * [ is_connected] ( #is_connected )
22+ * [ is_websocket] ( #is_websocket )
2123 * [ query] ( #query )
2224 * [ reconnect] ( #reconnect )
2325 * [ run] ( #run )
3133 * [ emit] ( #emit )
3234 * [ no_join] ( #no_join )
3335 * [ Failed packages] ( #failed-packages )
36+ * [ WebSockets] ( #websockets )
3437---------------------------------------
3538
3639## Installation
@@ -71,8 +74,7 @@ async def hello_world():
7174
7275 finally :
7376 # the will close the client in a nice way
74- client.close()
75- await client.wait_closed()
77+ await client.close_and_wait()
7678
7779# run the hello world example
7880asyncio.get_event_loop().run_until_complete(hello_world())
@@ -148,6 +150,16 @@ This method will return immediately so the connection may not be
148150closed yet after a call to `close()` . Use the [wait_closed()](# wait_closed) method
149151after calling this method if this is required.
150152
153+ # ## close_and_wait
154+
155+ ```python
156+ async Client().close_and_wait() -> None
157+ ```
158+
159+ Close and wait for the the connection to be closed.
160+
161+ This is equivalent of combining [close()](# close)) and [wait_closed()](#wait_closed).
162+
151163# ## connect
152164
153165```python
@@ -167,11 +179,12 @@ connection before using the connection.
167179# ### Args
168180
169181- * host (str )* :
170- A hostname, IP address, FQDN to connect to.
182+ A hostname, IP address, FQDN or URI _( for WebSockets)_ to connect to.
171183- * port (int , optional)* :
172184 Integer value between 0 and 65535 and should be the port number
173185 where a ThingsDB node is listening to for client connections.
174- Defaults to 9200 .
186+ Defaults to 9200 . For WebSocket connections the port must be
187+ provided with the URI _(see host argument)_.
175188- * timeout (int , optional)* :
176189 Can be be used to control the maximum time the client will
177190 attempt to create a connection. The timeout may be set to
@@ -207,17 +220,18 @@ to perform the authentication.
207220
208221```python
209222await connect_pool([
210- ' node01.local' , # address as string
211- ' node02.local' , # port will default to 9200
212- (' node03.local' , 9201 ), # ..or with an explicit port
223+ ' node01.local' , # address or WebSocket URI as string
224+ ' node02.local' , # port will default to 9200 or ignored for URI
225+ (' node03.local' , 9201 ), # ..or with an explicit port (ignored for URI)
213226], " admin" , " pass" )
214227```
215228
216229# ### Args
217230
218231- * pool (list of addresses)* :
219232 Should be an iterable with node address strings, or tuples
220- with `address` and `port` combinations in a tuple or list .
233+ with `address` and `port` combinations in a tuple or list . For WebSockets,
234+ the address must be an URI with the port included. (e.g: `" ws://host:9270" ` )
221235- * \*auth (str or (str, str))*:
222236 Argument `auth` can be be either a string with a token or a
223237 tuple with username and password. (the latter may be provided
@@ -282,6 +296,18 @@ Can be used to check if the client is connected.
282296# ### Returns
283297`True ` when the client is connected else `False ` .
284298
299+
300+ # ## is_websocket
301+
302+ ```python
303+ Client().is_websocket() -> bool
304+ ```
305+
306+ Can be used to check if the client is using a WebSocket connection.
307+
308+ # ### Returns
309+ `True ` when the client is connected else `False ` .
310+
285311# ## query
286312
287313```python
@@ -595,3 +621,52 @@ set_package_fail_file('/tmp/thingsdb-invalid-data.mp')
595621# When a package is received which fails to unpack, the data from this package
596622# will be stored to file.
597623```
624+
625+
626+ # # WebSockets
627+
628+ Since ThingsDB 1.6 has received WebSocket support. The Python client is able to use the WebSockets protocol by providing the `host` as URI .
629+ For WebSocket connections,the `port` argument will be ignored and must be specified with the URI instead.
630+
631+ Default the `websockets` package is ** not included** when installing this connector.
632+
633+ If you want to use WebSockets, make sure to install the package:
634+
635+ ```
636+ pip install websockets
637+ ```
638+
639+ For example:
640+
641+ ```python
642+ import asyncio
643+ from thingsdb.client import Client
644+
645+ async def hello_world():
646+ client = Client()
647+
648+ # replace `ws://localhost:9270` with your URI
649+ await client.connect(' ws://localhost:9270' )
650+
651+ # for a secure connection, use wss:// and provide an SSL context, example:
652+ # (ssl can be set either to True or False, or an SSLContext)
653+ #
654+ # await client.connect('wss://localhost:9270', ssl=True)
655+
656+ try :
657+ # replace `admin` and `pass` with your username and password
658+ # or use a valid token string
659+ await client.authenticate(' admin' , ' pass' )
660+
661+ # perform the hello world code...
662+ print (await client.query('''
663+ "Hello World!";
664+ ''' )
665+
666+ finally :
667+ # the will close the client in a nice way
668+ await client.close_and_wait()
669+
670+ # run the hello world example
671+ asyncio.get_event_loop().run_until_complete(hello_world())
672+ ```
0 commit comments