websocket

class platypush.plugins.websocket.WebsocketPlugin(subscriptions: Optional[Collection[str]] = None, **kwargs)[source]

Bases: AsyncRunnablePlugin

Plugin to send and receive messages over websocket connections.

Triggers:

__init__(subscriptions: Optional[Collection[str]] = None, **kwargs)[source]
Parameters:

subscriptions – List of websocket URLs that should be subscribed at startup, prefixed by ws:// or wss://.

recv(url: str, ssl_cert=None, ssl_key=None, ssl_cafile=None, ssl_capath=None, num_messages=0, timeout=0)[source]

Receive one or more messages from a websocket.

A platypush.message.event.websocket.WebsocketMessageEvent event will be triggered whenever a new message is received.

Parameters:
  • url – Websocket URL, e.g. ws://localhost:8765 or wss://localhost:8765

  • ssl_cert – Path to the SSL certificate to be used, if the SSL connection requires client authentication as well (default: None)

  • ssl_key – Path to the SSL key to be used, if the SSL connection requires client authentication as well (default: None)

  • ssl_cafile – Path to the certificate authority file if required by the SSL configuration (default: None)

  • ssl_capath – Path to the certificate authority directory if required by the SSL configuration (default: None)

  • num_messages – Exit after receiving this number of messages. Default: 0, receive forever.

  • timeout – Message receive timeout in seconds. Default: 0 - no timeout.

Returns:

A list with the messages that have been received, unless num_messages is set to 0 or None.

send(url: str, msg, ssl_cert=None, ssl_key=None, ssl_cafile=None, ssl_capath=None, wait_response=False)[source]

Sends a message to a websocket.

Parameters:
  • url – Websocket URL, e.g. ws://localhost:8765 or wss://localhost:8765

  • msg – Message to be sent. It can be a list, a dict, or a Message object

  • ssl_cert – Path to the SSL certificate to be used, if the SSL connection requires client authentication as well (default: None)

  • ssl_key – Path to the SSL key to be used, if the SSL connection requires client authentication as well (default: None)

  • ssl_cafile – Path to the certificate authority file if required by the SSL configuration (default: None)

  • ssl_capath – Path to the certificate authority directory if required by the SSL configuration (default: None)

  • wait_response – Set to True if you expect a response to the delivered message.

Returns:

The received response if wait_response is set to True, otherwise nothing.