1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
|
WebSocket Programming
=====================
This guide introduces WebSocket programming with |Ab|.
You'll see how to create WebSocket server (":ref:`creating-websocket-servers`") and client applications (":ref:`creating-websocket-clients`").
*Resources:*
* Example Code for this Guide: `Twisted-based <https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo>`_ or `asyncio-based <https://github.com/crossbario/autobahn-python/tree/master/examples/asyncio/websocket/echo>`_
* More :ref:`WebSocket Examples <websocket_examples>`
.. _creating-websocket-servers:
Creating Servers
----------------
Using |Ab| you can create WebSocket servers that will be able to talk to any (compliant) WebSocket client, including browsers.
We'll cover how to define the behavior of your WebSocket server by writing *protocol classes* and show some boilerplate for actually running a WebSocket server using the behavior defined in the server protocol.
Server Protocols
~~~~~~~~~~~~~~~~
To create a WebSocket server, you need to **write a protocol class to specify the behavior** of the server.
For example, here is a protocol class for a WebSocket echo server that will simply echo back any WebSocket message it receives:
.. code-block:: python
class MyServerProtocol(WebSocketServerProtocol):
def onMessage(self, payload, isBinary):
## echo back message verbatim
self.sendMessage(payload, isBinary)
This is just three lines of code, but we will go through each one carefully, since writing protocol classes like above really is core to WebSocket programming using |ab|.
The **first thing** to note is that you **derive** your protocol class from a base class provided by |ab|. Depending on whether you write a Twisted or a asyncio based application, here are the base classes to derive from:
* :class:`autobahn.twisted.websocket.WebSocketServerProtocol`
* :class:`autobahn.asyncio.websocket.WebSocketServerProtocol`
So a Twisted-based echo protocol would import the base protocol from ``autobahn.twisted.websocket`` and derive from :class:`autobahn.twisted.websocket.WebSocketServerProtocol`
*Twisted:*
.. code-block:: python
from autobahn.twisted.websocket import WebSocketServerProtocol
class MyServerProtocol(WebSocketServerProtocol):
def onMessage(self, payload, isBinary):
## echo back message verbatim
self.sendMessage(payload, isBinary)
while an asyncio echo protocol would import the base protocol from ``autobahn.asyncio.websocket`` and derive from :class:`autobahn.asyncio.websocket.WebSocketServerProtocol`
*asyncio:*
.. code-block:: python
from autobahn.asyncio.websocket import WebSocketServerProtocol
class MyServerProtocol(WebSocketServerProtocol):
def onMessage(self, payload, isBinary):
## echo back message verbatim
self.sendMessage(payload, isBinary)
.. note:: In this example, only the imports differ between the Twisted and the asyncio variant. The rest of the code is identical. However, in most real world programs you probably won't be able to or don't want to avoid using network framework specific code.
----------
.. _receiving-messages:
Receiving Messages
~~~~~~~~~~~~~~~~~~
The **second thing** to note is that we **override a callback** ``onMessage`` which is called by |ab| whenever the callback related event happens.
In case of ``onMessage``, the callback will be called whenever a new WebSocket message was received. There are more WebSocket related callbacks, but for now the ``onMessage`` callback is all we need.
When our server receives a WebSocket message, the :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onMessage` will fire with the message ``payload`` received.
The ``payload`` is always a Python byte string. Since WebSocket is able to transmit **text** (UTF8) and **binary** payload, the actual payload type is signaled via the ``isBinary`` flag.
When the ``payload`` is **text** (``isBinary == False``), the bytes received will be an UTF8 encoded string. To process **text** payloads, the first thing you often will do is decoding the UTF8 payload into a Python string:
.. code-block:: python
s = payload.decode('utf8')
.. tip::
You don't need to validate the bytes for actually being valid UTF8 - |ab| does that already when receiving the message.
When using WebSocket text messages with JSON ``payload``, typical code for receiving and decoding messages into Python objects that works on both Python 2 and 3 would look like this:
.. code-block:: python
import json
obj = json.loads(payload.decode('utf8'))
We are using the Python standard JSON module :py:mod:`json`.
The ``payload`` (which is of type ``bytes`` on Python 3 and ``str`` on Python 2) is decoded from UTF8 into a native Python string, and then parsed from JSON into a native Python object.
----------
.. _sending-messages:
Sending Messages
~~~~~~~~~~~~~~~~
The **third thing** to note is that we **use methods** like ``sendMessage`` provided by the base class to perform WebSocket related actions, like sending a WebSocket message.
As there are more methods for performing other actions (like closing the connection), we'll come back to this later, but for now, the ``sendMessage`` method is all we need.
:meth:`autobahn.websocket.interfaces.IWebSocketChannel.sendMessage` takes the ``payload`` to send in a WebSocket message as Python bytes. Since WebSocket is able to transmit payloads of **text** (UTF8) and **binary** type, you need to tell |ab| the actual type of the ``payload`` bytes. This is done using the ``isBinary`` flag.
Hence, to send a WebSocket text message, you will usually *encode* the payload to UTF8:
.. code-block:: python
payload = s.encode('utf8')
self.sendMessage(payload, isBinary = False)
.. warning::
|ab| will NOT validate the bytes of a text ``payload`` being sent for actually being valid UTF8. You MUST ensure that you only provide valid UTF8 when sending text messages. If you produce invalid UTF8, a conforming WebSocket peer will close the WebSocket connection due to the protocol violation.
When using WebSocket text messages with JSON ``payload``, typical code for encoding and sending Python objects that works on both Python 2 and 3 would look like this:
.. code-block:: python
import json
payload = json.dumps(obj, ensure_ascii = False).encode('utf8')
We are using the Python standard JSON module :py:mod:`json`.
The ``ensure_ascii == False`` option allows the JSON serializer to use Unicode strings. We can do this since we are encoding to UTF8 afterwards anyway. And UTF8 can represent the full Unicode character set.
----------
Running a Server
~~~~~~~~~~~~~~~~
Now that we have defined the behavior of our WebSocket server in a protocol class, we need to actually start a server based on that behavior.
Doing so involves two steps:
1. Create a **Factory** for producing instances of our protocol class
2. Create a TCP **listening server** using the former Factory
Here is one way of doing that when using Twisted
*Twisted:*
.. code-block:: python
:emphasize-lines: 9-11
if __name__ == '__main__':
import sys
from twisted.python import log
from twisted.internet import reactor
log.startLogging(sys.stdout)
from autobahn.twisted.websocket import WebSocketServerFactory
factory = WebSocketServerFactory()
factory.protocol = MyServerProtocol
reactor.listenTCP(9000, factory)
reactor.run()
What we are doing here is
1. Setup Twisted logging
2. Create a :class:`autobahn.twisted.websocket.WebSocketServerFactory` and set our ``MyServerProtocol`` on the factory (the highlighted lines)
3. Start a server using the factory, listening on TCP port 9000
Similar, here is the asyncio way
*asyncio:*
.. code-block:: python
:emphasize-lines: 9-11
if __name__ == '__main__':
try:
import asyncio
except ImportError:
## Trollius >= 0.3 was renamed
import trollius as asyncio
from autobahn.asyncio.websocket import WebSocketServerFactory
factory = WebSocketServerFactory()
factory.protocol = MyServerProtocol
loop = asyncio.get_event_loop()
coro = loop.create_server(factory, '127.0.0.1', 9000)
server = loop.run_until_complete(coro)
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
server.close()
loop.close()
What we are doing here is
1. Import asyncio, or the Trollius backport
2. Create a :class:`autobahn.asyncio.websocket.WebSocketServerFactory` and set our ``MyServerProtocol`` on the factory (the highlighted lines)
3. Start a server using the factory, listening on TCP port 9000
.. note::
As can be seen, the boilerplate to create and run a server differ from Twisted, but the core code of creating a factory and setting our protocol (the highlighted lines) is identical (other than the differing import for the WebSocket factory).
You can find complete code for above examples here:
* `WebSocket Echo (Twisted-based) <https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo>`_
* `WebSocket Echo (Asyncio-based) <https://github.com/crossbario/autobahn-python/tree/master/examples/asyncio/websocket/echo>`_
.. _connection-lifecycle:
Connection Lifecycle
--------------------
As we have seen above, |ab| will fire *callbacks* on your protocol class whenever the event related to the respective callback occurs.
It is in these callbacks that you will implement application specific code.
The core WebSocket interface :class:`autobahn.websocket.interfaces.IWebSocketChannel` provides the following *callbacks*:
* :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onConnect`
* :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onOpen`
* :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onMessage`
* :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onClose`
We have already seen the callback for :ref:`receiving-messages`. This callback will usually fire many times during the lifetime of a WebSocket connection.
In contrast, the other three callbacks above each only fires once for a given connection.
Opening Handshake
~~~~~~~~~~~~~~~~~
Whenever a new client connects to the server, a new protocol instance will be created and the :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onConnect` callback fires as soon as the WebSocket opening handshake is begun by the client.
For a WebSocket server protocol, ``onConnect()`` will fire with
:class:`autobahn.websocket.protocol.ConnectionRequest` providing information on the client wishing to connect via WebSocket.
.. code-block:: python
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("Client connecting: {}".format(request.peer))
On the other hand, for a WebSocket client protocol, ``onConnect()`` will fire with
:class:`autobahn.websocket.protocol.ConnectionResponse` providing information on the WebSocket connection that was accepted by the server.
.. code-block:: python
class MyClientProtocol(WebSocketClientProtocol):
def onConnect(self, response):
print("Connected to Server: {}".format(response.peer))
In this callback you can do things like
* checking or setting cookies or other HTTP headers
* verifying the client IP address
* checking the origin of the WebSocket request
* negotiate WebSocket subprotocols
For example, a WebSocket client might offer to speak several WebSocket subprotocols. The server can inspect the offered protocols in ``onConnect()`` via the supplied instance of :class:`autobahn.websocket.protocol.ConnectionRequest`. When the server accepts the client, it'll chose one of the offered subprotocols. The client can then inspect the selected subprotocol in it's ``onConnect()`` callback in the supplied instance of :class:`autobahn.websocket.protocol.ConnectionResponse`.
Connection Open
~~~~~~~~~~~~~~~
The :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onOpen` callback fires when the WebSocket opening handshake has been successfully completed. You now can send and receive messages over the connection.
.. code-block:: python
class MyProtocol(WebSocketProtocol):
def onOpen(self):
print("WebSocket connection open.")
Closing a Connection
~~~~~~~~~~~~~~~~~~~~
The core WebSocket interface :class:`autobahn.websocket.interfaces.IWebSocketChannel` provides the following *methods*:
* :meth:`autobahn.websocket.interfaces.IWebSocketChannel.sendMessage`
* :meth:`autobahn.websocket.interfaces.IWebSocketChannel.sendClose`
We've already seen one of above in :ref:`sending-messages`.
The :meth:`autobahn.websocket.interfaces.IWebSocketChannel.sendClose` will initiate a WebSocket closing handshake. After starting to close a WebSocket connection, no messages can be sent. Eventually, the :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onClose` callback will fire.
After a WebSocket connection has been closed, the protocol instance will get recycled. Should the client reconnect, a new protocol instance will be created and a new WebSocket opening handshake performed.
Connection Close
~~~~~~~~~~~~~~~~
When the WebSocket connection has closed, the :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onClose` callback fires.
.. code-block:: python
class MyProtocol(WebSocketProtocol):
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {}".format(reason))
When the connection has closed, no messages will be received anymore and you cannot send messages also. The protocol instance won't be reused. It'll be garbage collected. When the client reconnects, a completely new protocol instance will be created.
.. _creating-websocket-clients:
Creating Clients
----------------
.. note::
Creating WebSocket clients using |Ab| works very similar to creating WebSocket servers. Hence you should have read through :ref:`creating-websocket-servers` first.
As with servers, the behavior of your WebSocket client is defined by writing a *protocol class*.
Client Protocols
~~~~~~~~~~~~~~~~
To create a WebSocket client, you need to write a protocol class to **specify the behavior** of the client.
For example, here is a protocol class for a WebSocket client that will send a WebSocket text message as soon as it is connected and log any WebSocket messages it receives:
.. code-block:: python
class MyClientProtocol(WebSocketClientProtocol):
def onOpen(self):
self.sendMessage(u"Hello, world!".encode('utf8'))
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
Similar to WebSocket servers, you **derive** your WebSocket client protocol class from a base class provided by |ab|. Depending on whether you write a Twisted or a asyncio based application, here are the base classes to derive from:
* :class:`autobahn.twisted.websocket.WebSocketClientProtocol`
* :class:`autobahn.asyncio.websocket.WebSocketClientProtocol`
So a Twisted-based protocol would import the base protocol from ``autobahn.twisted.websocket`` and derive from :class:`autobahn.twisted.websocket.WebSocketClientProtocol`
*Twisted:*
.. code-block:: python
from autobahn.twisted.websocket import WebSocketClientProtocol
class MyClientProtocol(WebSocketClientProtocol):
def onOpen(self):
self.sendMessage(u"Hello, world!".encode('utf8'))
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
while an asyncio-based protocol would import the base protocol from ``autobahn.asyncio.websocket`` and derive from :class:`autobahn.asyncio.websocket.WebSocketClientProtocol`
*asyncio:*
.. code-block:: python
from autobahn.asyncio.websocket import WebSocketClientProtocol
class MyClientProtocol(WebSocketClientProtocol):
def onOpen(self):
self.sendMessage(u"Hello, world!".encode('utf8'))
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
.. note:: In this example, only the imports differs between the Twisted and the asyncio variant. The rest of the code is identical. However, in most real world programs you probably won't be able to or don't want to avoid using network framework specific code.
-------
Receiving and sending WebSocket messages as well as connection lifecycle in clients works exactly the same as with servers. Please see
* :ref:`receiving-messages`
* :ref:`sending-messages`
* :ref:`connection-lifecycle`
Running a Client
~~~~~~~~~~~~~~~~
Now that we have defined the behavior of our WebSocket client in a protocol class, we need to actually start a client based on that behavior.
Doing so involves two steps:
1. Create a **Factory** for producing instances of our protocol class
2. Create a TCP **connecting client** using the former Factory
Here is one way of doing that when using Twisted
*Twisted:*
.. code-block:: python
:emphasize-lines: 9-11
if __name__ == '__main__':
import sys
from twisted.python import log
from twisted.internet import reactor
log.startLogging(sys.stdout)
from autobahn.twisted.websocket import WebSocketClientFactory
factory = WebSocketClientFactory()
factory.protocol = MyClientProtocol
reactor.connectTCP("127.0.0.1", 9000, factory)
reactor.run()
What we are doing here is
1. Setup Twisted logging
2. Create a :class:`autobahn.twisted.websocket.WebSocketClientFactory` and set our ``MyClientProtocol`` on the factory (the highlighted lines)
3. Start a client using the factory, connecting to localhost ``127.0.0.1`` on TCP port 9000
Similar, here is the asyncio way
*asyncio:*
.. code-block:: python
:emphasize-lines: 9-11
if __name__ == '__main__':
try:
import asyncio
except ImportError:
## Trollius >= 0.3 was renamed
import trollius as asyncio
from autobahn.asyncio.websocket import WebSocketClientFactory
factory = WebSocketClientFactory()
factory.protocol = MyClientProtocol
loop = asyncio.get_event_loop()
coro = loop.create_connection(factory, '127.0.0.1', 9000)
loop.run_until_complete(coro)
loop.run_forever()
loop.close()
What we are doing here is
1. Import asyncio, or the Trollius backport
2. Create a :class:`autobahn.asyncio.websocket.WebSocketClientFactory` and set our ``MyClientProtocol`` on the factory (the highlighted lines)
3. Start a client using the factory, connecting to localhost ``127.0.0.1`` on TCP port 9000
.. note::
As can be seen, the boilerplate to create and run a client differ from Twisted, but the core code of creating a factory and setting our protocol (the highlighted lines) is identical (other than the differing import for the WebSocket factory).
You can find complete code for above examples here:
* `WebSocket Echo (Twisted-based) <https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo>`_
* `WebSocket Echo (Asyncio-based) <https://github.com/crossbario/autobahn-python/tree/master/examples/asyncio/websocket/echo>`_
WebSocket Options
-----------------
You can pass various options on both client and server side WebSockets; these are accomplished by calling :meth:`autobahn.websocket.WebSocketServerFactory.setProtocolOptions` or :meth:`autobahn.websocket.WebSocketClientFactory.setProtocolOptions` with keyword arguments for each option.
Common Options (server and client)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- logOctets: if True, log every byte
- logFrames: if True, log information about each frame
- trackTimings: if True, enable debug timing code
- utf8validateIncoming: if True (default), validate all incoming UTF8
- applyMask: if True (default) apply mask to frames, when available
- maxFramePayloadSize: if 0 (default), unlimited-sized frames allowed
- maxMessagePayloadSize: if 0 (default), unlimited re-assembled payloads
- autoFragmentSize: if 0 (default), don't fragment
- failByDrop: if True (default), failed connections are terminated immediately
- echoCloseCodeReason: if True, echo back the close reason/code
- openHandshakeTimeout: timeout in seconds after which opening handshake will be failed (default: no timeout)
- closeHandshakeTimeout: timeout in seconds after which close handshake will be failed (default: no timeout)
- tcpNoDelay: if True (default), set NODELAY (Nagle) socket option
- autoPingInterval: if set, seconds between auto-pings
- autoPingTimeout: if set, seconds until a ping is considered timed-out
- autoPingSize: bytes of random data to send in ping messages (between 4 [default] and 125)
Server-Only Options
~~~~~~~~~~~~~~~~~~~
- versions: what versions to claim support for (default 8, 13)
- webStatus: if True (default), show a web page if visiting this endpoint without an Upgrade header
- requireMaskedClientFrames: if True (default), client-to-server frames must be masked
- maskServerFrames: if True, server-to-client frames must be masked
- perMessageCompressionAccept: if provided, a single-argument callable
- serveFlashSocketPolicy: if True, server a flash policy file (default: False)
- flashSocketPolicy: the actual flash policy to serve (default one allows everything)
- allowedOrigins: a list of origins to allow, with embedded `*`'s for wildcards; these are turned into regular expressions (e.g. `https://*.example.com:443` becomes `^https://.*\.example\.com:443$`). When doing the matching, the origin is **always** of the form `scheme://host:port` with an explicit port. By default, we match with `*` (that is, anything). To match all subdomains of `example.com` on any scheme and port, you'd need `*://*.example.com:*`
- maxConnections: total concurrent connections allowed (default 0, unlimited)
- trustXForwardedFor: number of trusted web servers (reverse proxies) in front of this server which set the X-Forwarded-For header
Client-Only Options
~~~~~~~~~~~~~~~~~~~
- version: which version we are (default: 18)
- acceptMaskedServerFrames: if True, accept masked server-to-client frames (default False)
- maskClientFrames: if True (default), mask client-to-server frames
- serverConnectionDropTimeout: how long (in seconds) to wait for server to drop the connection when closing (default 1)
- perMessageCompressionOffers:
- perMessageCompressionAccept:
Upgrading
---------
From < 0.7.0
~~~~~~~~~~~~
Starting with release 0.7.0, |Ab| now supports both Twisted and asyncio as the underlying network library. This required renaming some modules.
Hence, code for |ab| **< 0.7.0**
.. code-block:: python
from autobahn.websocket import WebSocketServerProtocol
should be modified for |ab| **>= 0.7.0** for (using Twisted)
.. code-block:: python
from autobahn.twisted.websocket import WebSocketServerProtocol
or (using asyncio)
.. code-block:: python
from autobahn.asyncio.websocket import WebSocketServerProtocol
Two more small changes:
1. The method ``WebSocketProtocol.sendMessage`` had parameter ``binary`` renamed to ``isBinary`` (for consistency with ``onMessage``)
2. The ``ConnectionRequest`` object no longer provides ``peerstr``, but only ``peer``, and the latter is a plain, descriptive string (this was needed since we now support both Twisted and asyncio, and also non-TCP transports)
|