Files
deb-python-autobahn/doc/websocketintro.rst
Alexander Goedde d9e1a2b8b1 doc cleanup + fixes
2014-03-25 13:40:08 +01:00

4.8 KiB

WebSocket Introduction

Basic WebSocket

AutobahnPython provides a Message-based API to WebSocket plus auxiliary methods and callbacks.

The message-based API closely resembles the API of WebSocket available to JavaScript in browsers.

Most of the time, this API is what you should use, unless you have specific needs (frame-based / streaming processing, see below).

Message-based Processing

The message-based API is implemented in the following methods and callbacks

  • autobahn.websocket.protocol.WebSocketProtocol.onOpen
  • autobahn.websocket.protocol.WebSocketProtocol.sendMessage
  • autobahn.websocket.protocol.WebSocketProtocol.onMessage
  • autobahn.websocket.protocol.WebSocketProtocol.sendClose
  • autobahn.websocket.protocol.WebSocketProtocol.onClose

Prepared Messages

In case you want to send a single WebSocket message to multiple peers, AutobahnPython provides an optimized way of sending using

  • autobahn.websocket.protocol.WebSocketFactory.prepareMessage
  • autobahn.websocket.protocol.WebSocketProtocol.sendPreparedMessage

Handshake Hooks

AutobahnPython allows you to hook into the initial WebSocket opening handshake (e.g. for handling HTTP cookies, subprotocols, etc):

  • autobahn.websocket.protocol.WebSocketServerProtocol.onConnect
  • autobahn.websocket.protocol.WebSocketClientProtocol.onConnect

Ping/Pong Processing

The basic API also allows for explicit processing of WebSocket Pings and Pongs:

  • autobahn.websocket.protocol.WebSocketProtocol.onPing
  • autobahn.websocket.protocol.WebSocketProtocol.onPong
  • autobahn.websocket.protocol.WebSocketProtocol.sendPing
  • autobahn.websocket.protocol.WebSocketProtocol.sendPong

Note that explicit processing of Pings/Pongs is unnecessary normally - AutobahnPython will do the right thing under the hood.

Interface Definition

The basic API has the following interface definition:

autobahn.websocket.interfaces.IWebSocketChannel

Advanced WebSocket

A WebSockets message consists of a potentially unlimited number of fragments ("message frames"), each of which can have a payload between 0 and 2^63 octets.

The implementation of the basic API is message-based, and thus has to buffer all data received for a message frame, and buffer all frames received for a message, and only when the message finally ends, flattens all buffered data and fires autobahn.websocket.protocol.WebSocketProtocol.onMessage.

Usually, when you produce/consume messages of small to limited size (like say <256k), this is absolutely sufficient and convenient.

However, when you want to process messages consisting of a large number of message fragments, or you want to process messages that contain message fragments of large size, this buffering will result in excessive memory consumption.

In these cases, you might want to process message fragments on a per frame basis, or you may even want to process data incoming, as it arrives.

The advanced API provides you all the necessary methods and callbacks to do WebSockets using frame-based processing or even completely streaming processing - both sending and receiving.

Frame-based API

API for frame-based processing is implemented in:

  • autobahn.websocket.protocol.WebSocketProtocol.onMessageBegin
  • autobahn.websocket.protocol.WebSocketProtocol.onMessageFrame
  • autobahn.websocket.protocol.WebSocketProtocol.onMessageEnd
  • autobahn.websocket.protocol.WebSocketProtocol.beginMessage
  • autobahn.websocket.protocol.WebSocketProtocol.sendMessageFrame
  • autobahn.websocket.protocol.WebSocketProtocol.endMessage

and has the following definition:

autobahn.websocket.interfaces.IWebSocketChannelFrameApi

Streaming API

API for streaming processing is implemented in:

  • autobahn.websocket.protocol.WebSocketProtocol.onMessageBegin
  • autobahn.websocket.protocol.WebSocketProtocol.onMessageFrameBegin
  • autobahn.websocket.protocol.WebSocketProtocol.onMessageFrameData
  • autobahn.websocket.protocol.WebSocketProtocol.onMessageFrameEnd
  • autobahn.websocket.protocol.WebSocketProtocol.onMessageEnd
  • autobahn.websocket.protocol.WebSocketProtocol.beginMessage
  • autobahn.websocket.protocol.WebSocketProtocol.beginMessageFrame
  • autobahn.websocket.protocol.WebSocketProtocol.sendMessageFrameData
  • autobahn.websocket.protocol.WebSocketProtocol.endMessage

and has the following definition:

autobahn.websocket.interfaces.IWebSocketChannelStreamingApi