The class autobahn.websocket.WebSocketProtocol implements the WebSocket protocol.
Though you will use methods and override callbacks from this class, you must implement your clients/servers by deriving from the classes autobahn.websocket.WebSocketClientProtocol and autobahn.websocket.WebSocketServerProtocol.
Most of the time, the basic API offered by AutobahnPython will be the one you want to use. It is easy to use and gets out of your way.
The basic API is the one to use, unless you have specific needs (frame-based processing or streaming), in which case Autobahn provides an advanced API (see below).
The basic API consists of the following methods and callbacks
- autobahn.websocket.WebSocketProtocol.onOpen()
- autobahn.websocket.WebSocketProtocol.onMessage()
- autobahn.websocket.WebSocketProtocol.onClose()
- autobahn.websocket.WebSocketProtocol.sendMessage()
- autobahn.websocket.WebSocketProtocol.sendClose()
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.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.
API for frame-based processing:
- autobahn.websocket.WebSocketProtocol.onMessageBegin()
- autobahn.websocket.WebSocketProtocol.onMessageFrame()
- autobahn.websocket.WebSocketProtocol.onMessageEnd()
- autobahn.websocket.WebSocketProtocol.beginMessage()
- autobahn.websocket.WebSocketProtocol.sendMessageFrame()
- autobahn.websocket.WebSocketProtocol.endMessage()
API for streaming processing:
- autobahn.websocket.WebSocketProtocol.onMessageBegin()
- autobahn.websocket.WebSocketProtocol.onMessageFrameBegin()
- autobahn.websocket.WebSocketProtocol.onMessageFrameData()
- autobahn.websocket.WebSocketProtocol.onMessageFrameEnd()
- autobahn.websocket.WebSocketProtocol.onMessageEnd()
- autobahn.websocket.WebSocketProtocol.beginMessage()
- autobahn.websocket.WebSocketProtocol.beginMessageFrame()
- autobahn.websocket.WebSocketProtocol.sendMessageFrameData()
- autobahn.websocket.WebSocketProtocol.endMessage()
The advanced API for frame-based/streaming processing of WebSockets messages also provides access to extension points in the WebSockets protocol (you also normally won’t use) - namely “reserved bits” and “reserved opcodes”.
Additionally, the advanced API provides methods and callbacks to do your own processing of WebSockets Pings and Pongs. Normally, it is unnecessary to do that, Autobahn will do the right thing under the hood. Anyway, if you want, you can do.
API for explicit Ping/Pong processing:
- autobahn.websocket.WebSocketProtocol.onPing()
- autobahn.websocket.WebSocketProtocol.onPong()
- autobahn.websocket.WebSocketProtocol.sendPing()
- autobahn.websocket.WebSocketProtocol.sendPong()