<commit_before>.. _sec-jsclientlib-socket:

:mod:`OctoPrintClient.socket`
-----------------------------

.. js:attribute:: OctoPrintClient.socket.options

   The socket client's options.

   ``OctoPrintClient.socket.options.timeouts``
       A list of consecutive timeouts after which to attempt reconnecting to a
       disconnected sockets, in seconds. Defaults to ``[1, 1, 2, 3, 5, 8, 13, 20, 40, 100]``.
       The default setting here makes the client slowly back off after the first couple of very
       fast connection attempts don't succeed, and give up after 10 tries.

   ``OctoPrintClient.socket.options.rateSlidingWindowSize``
       Number of last rate measurements to take into account for timing analysis and
       communication throttling. See :ref:`Communication Throttling <sec-jsclient-socket-throttling>`
       below.

.. js:function:: OctoPrintClient.socket.connect(opts)

   Connects the socket client to OctoPrint's `SockJS <http://sockjs.org/>`_ socket.

   The optional parameter ``opts`` may be used to provide additional configuration options
   to the SockJS constructor. See the `SockJS documentation <https://github.com/sockjs/sockjs-client#sockjs-class>`_ on potential options.

   :param object opts: Additional options for the SockJS constructor.

.. js:function:: OctoPrintClient.socket.reconnect()

   Reconnects the socket client. If the socket is currently connected it will be disconnected first.

.. js:function:: OctoPrintClient.socket.disconnect()

   Disconnects the socket client.

.. js:function:: OctoPrintClient.socket.onMessage(message, handler)

   Registers the ``handler`` for messages of type ``message``.

   To register for all message types, provide ``*`` as the type to register for.

   ``handler`` is expected to be a function accepting one object parameter ``eventObj``, consisting
   of the received message as property ``event`` and the received payload (if any) as property ``data``.

   .. code-block:: javascript

      OctoPrint.socket.onMessage("*", function(message) {
          // do something with the message object
      });

   The socket client will measure how long message processing over all handlers will take and utilize
   that measurement to determine if the :ref:`communication throttling <sec-jsclient-socket-throttling>`
   needs to be adjusted or not.

   Please refer to the :ref:`Push API documentation <sec-api-push>`
   for details on the possible message types and their payloads.

   :param string message: The type of message for which to register
   :param function handler: The handler function

.. js:function:: OctoPrintClient.socket.sendMessage(type, payload)

   Sends a message of type ``type`` with the provided ``payload`` to the server.

   Note that at the time of writing, OctoPrint only supports the ``throttle`` and ``auth`` messages. See
   also the :ref:`Push API documentation <sec-api-push>`.

   :param string type: Type of message to send
   :param object payload: Payload to send

.. js:function:: OctoPrintClient.socket.sendAuth(userId, session)

   Sends an ``auth`` message with the provided ``userId`` and ``session`` to the server.

   ``session`` is expected to be the ``session`` value retrieved
   from any valid :ref:`OctoPrint.browser.login(userId,...) <sec-jsclientlib-browser>` response.

   See also the :ref:`Push API documentation <sec-api-push>`.

   :param string userId: An existing OctoPrint username
   :param string session: A valid session id for the provided username

.. js:function:: OctoPrintClient.socket.onRateTooLow(measured, minimum)

   Called by the socket client when the measured message round trip times have been lower than
   the current lower processing limit over the full sliding window, indicating that messages
   are now processed faster than the current rate and a faster rate might be possible.

   Can be overwritten with custom handler methods. The default implementation will call
   :js:func:`OctoPrint.socket.increaseRate`.

   :param Number measured: Maximal measured message round trip time
   :param Number minimum: Lower round trip time limit for keeping the rate

.. js:function:: OctoPrintClient.socket.onRateTooHigh(measured, maximum)

   Called by the socket client when the last measured round trip time was higher than the
   current upper processing limit, indicating that the messages are now processed slower than
   the current rate requires and a slower rate might be necessary.

   Can be overwritten with custom handler methods. The default implementation will call
   :js:func:`OctoPrint.socket.decreaseRate`.

   :param Number measured: Measured message round trip time
   :param Number minimum: Upper round trip time limit for keeping the rate

.. js:function:: OctoPrintClient.socket.increaseRate()

   Instructs the server to increase the message rate by 500ms.

.. js:function:: OctoPrintClient.socket.decreaseRate()

   Instructs the server to decrease the message rate by 500ms.

Sample to setup an authed socket
================================

.. code-block:: javascript

      OctoPrint.socket.connect();
      OctoPrint.browser.login("myusername", "mypassword", true)
          .done(function(response) {
              client.socket.sendAuth("myusername", response.session);
          });

.. _sec-jsclient-socket-throttling:

Communication Throttling
========================

The socket client supports communication throttling. It will measure how long each incoming message takes
to be processed by all registered handlers. If the processing times in a sliding window are longer than
the current rate limit configured on the socket (default: 500ms between messages), the socket client will
instruct the server to send slower. If the messages are handled faster than half the current rate limit,
the socket client will instruct the server to send faster.<commit_msg>Fix typo as there is no instanciated client in the sample...<commit_after>.. _sec-jsclientlib-socket:

:mod:`OctoPrintClient.socket`
-----------------------------

.. js:attribute:: OctoPrintClient.socket.options

   The socket client's options.

   ``OctoPrintClient.socket.options.timeouts``
       A list of consecutive timeouts after which to attempt reconnecting to a
       disconnected sockets, in seconds. Defaults to ``[1, 1, 2, 3, 5, 8, 13, 20, 40, 100]``.
       The default setting here makes the client slowly back off after the first couple of very
       fast connection attempts don't succeed, and give up after 10 tries.

   ``OctoPrintClient.socket.options.rateSlidingWindowSize``
       Number of last rate measurements to take into account for timing analysis and
       communication throttling. See :ref:`Communication Throttling <sec-jsclient-socket-throttling>`
       below.

.. js:function:: OctoPrintClient.socket.connect(opts)

   Connects the socket client to OctoPrint's `SockJS <http://sockjs.org/>`_ socket.

   The optional parameter ``opts`` may be used to provide additional configuration options
   to the SockJS constructor. See the `SockJS documentation <https://github.com/sockjs/sockjs-client#sockjs-class>`_ on potential options.

   :param object opts: Additional options for the SockJS constructor.

.. js:function:: OctoPrintClient.socket.reconnect()

   Reconnects the socket client. If the socket is currently connected it will be disconnected first.

.. js:function:: OctoPrintClient.socket.disconnect()

   Disconnects the socket client.

.. js:function:: OctoPrintClient.socket.onMessage(message, handler)

   Registers the ``handler`` for messages of type ``message``.

   To register for all message types, provide ``*`` as the type to register for.

   ``handler`` is expected to be a function accepting one object parameter ``eventObj``, consisting
   of the received message as property ``event`` and the received payload (if any) as property ``data``.

   .. code-block:: javascript

      OctoPrint.socket.onMessage("*", function(message) {
          // do something with the message object
      });

   The socket client will measure how long message processing over all handlers will take and utilize
   that measurement to determine if the :ref:`communication throttling <sec-jsclient-socket-throttling>`
   needs to be adjusted or not.

   Please refer to the :ref:`Push API documentation <sec-api-push>`
   for details on the possible message types and their payloads.

   :param string message: The type of message for which to register
   :param function handler: The handler function

.. js:function:: OctoPrintClient.socket.sendMessage(type, payload)

   Sends a message of type ``type`` with the provided ``payload`` to the server.

   Note that at the time of writing, OctoPrint only supports the ``throttle`` and ``auth`` messages. See
   also the :ref:`Push API documentation <sec-api-push>`.

   :param string type: Type of message to send
   :param object payload: Payload to send

.. js:function:: OctoPrintClient.socket.sendAuth(userId, session)

   Sends an ``auth`` message with the provided ``userId`` and ``session`` to the server.

   ``session`` is expected to be the ``session`` value retrieved
   from any valid :ref:`OctoPrint.browser.login(userId,...) <sec-jsclientlib-browser>` response.

   See also the :ref:`Push API documentation <sec-api-push>`.

   :param string userId: An existing OctoPrint username
   :param string session: A valid session id for the provided username

.. js:function:: OctoPrintClient.socket.onRateTooLow(measured, minimum)

   Called by the socket client when the measured message round trip times have been lower than
   the current lower processing limit over the full sliding window, indicating that messages
   are now processed faster than the current rate and a faster rate might be possible.

   Can be overwritten with custom handler methods. The default implementation will call
   :js:func:`OctoPrint.socket.increaseRate`.

   :param Number measured: Maximal measured message round trip time
   :param Number minimum: Lower round trip time limit for keeping the rate

.. js:function:: OctoPrintClient.socket.onRateTooHigh(measured, maximum)

   Called by the socket client when the last measured round trip time was higher than the
   current upper processing limit, indicating that the messages are now processed slower than
   the current rate requires and a slower rate might be necessary.

   Can be overwritten with custom handler methods. The default implementation will call
   :js:func:`OctoPrint.socket.decreaseRate`.

   :param Number measured: Measured message round trip time
   :param Number minimum: Upper round trip time limit for keeping the rate

.. js:function:: OctoPrintClient.socket.increaseRate()

   Instructs the server to increase the message rate by 500ms.

.. js:function:: OctoPrintClient.socket.decreaseRate()

   Instructs the server to decrease the message rate by 500ms.

Sample to setup an authed socket
================================

.. code-block:: javascript

      OctoPrint.socket.connect();
      OctoPrint.browser.login("myusername", "<PASSWORD>", true)
          .done(function(response) {
              OctoPrint.socket.sendAuth("myusername", response.session);
          });

.. _sec-jsclient-socket-throttling:

Communication Throttling
========================

The socket client supports communication throttling. It will measure how long each incoming message takes
to be processed by all registered handlers. If the processing times in a sliding window are longer than
the current rate limit configured on the socket (default: 500ms between messages), the socket client will
instruct the server to send slower. If the messages are handled faster than half the current rate limit,
the socket client will instruct the server to send faster.
{'fraction_non_alphanumeric': 0.09043817934782608, 'fraction_numerical': 0.004415760869565217, 'mean_word_length': 3.9818104906937393, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 2, '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': '2944854', 'n_tokens_mistral': 3078, 'n_tokens_neox': 2901, 'n_words': 1341}