<commit_before>.. _aiohttp-client-websockets:

WebSockets Client
=================

.. highlight:: python

.. module:: aiohttp.websocket_client

.. versionadded:: 0.15


:mod:`aiohttp` works with client websockets out-of-the-box.

You have to use the :func:`ws_connect()` coroutine for client
websocket connection. It accepts a *url* as a first parameter and returns
:class:`ClientWebSocketResponse`, with that object you can communicate with
websocket server using response's methods:

.. code-block:: python

   ws = yield from aiohttp.ws_connect(
       'http://webscoket-server.org/endpoint')

   while True:
       msg = yield from ws.receive()

       if msg.tp == aiohttp.MsgType.text:
           if msg.data == 'close':
              yield from ws.close()
              break
           else:
              ws.send_str(data + '/answer')
       elif msg.tp == aiohttp.MsgType.closed:
           break
       elif msg.tp == aiohttp.MsgType.error:
           break

If you prefer to establish *websocket client connection* from
:class:`~aiohttp.client.ClientSession` object please use
:meth:`aiohttp.client.ClientSession.ws_connect` coroutine::

   session = aiohttp.ClientSession()
   ws = yield from session.ws_connect(
       'http://webscoket-server.org/endpoint')
<commit_msg>Fixed a typo in websockets example<commit_after>.. _aiohttp-client-websockets:

WebSockets Client
=================

.. highlight:: python

.. module:: aiohttp.websocket_client

.. versionadded:: 0.15


:mod:`aiohttp` works with client websockets out-of-the-box.

You have to use the :func:`ws_connect()` coroutine for client
websocket connection. It accepts a *url* as a first parameter and returns
:class:`ClientWebSocketResponse`, with that object you can communicate with
websocket server using response's methods:

.. code-block:: python

   ws = yield from aiohttp.ws_connect(
       'http://webscoket-server.org/endpoint')

   while True:
       msg = yield from ws.receive()

       if msg.tp == aiohttp.MsgType.text:
           if msg.data == 'close':
              yield from ws.close()
              break
           else:
              ws.send_str(msg.data + '/answer')
       elif msg.tp == aiohttp.MsgType.closed:
           break
       elif msg.tp == aiohttp.MsgType.error:
           break

If you prefer to establish *websocket client connection* from
:class:`~aiohttp.client.ClientSession` object please use
:meth:`aiohttp.client.ClientSession.ws_connect` coroutine::

   session = aiohttp.ClientSession()
   ws = yield from session.ws_connect(
       'http://webscoket-server.org/endpoint')

{'fraction_non_alphanumeric': 0.11898344243357721, 'fraction_numerical': 0.0023103581055063534, 'mean_word_length': 2.954337899543379, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1175913', 'n_tokens_mistral': 800, 'n_tokens_neox': 759, 'n_words': 217}