Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unittest Flask websocket server (Flask-SocketIO)

I wonder how can I unittest on my flask websocket server. My application supports quite many interface on REST API(Flask-restful) and Web Socket(Flask-SocketIO). All of websocket "emit" are proceed on celery process. I have problem unit-testing those websocket logic.

  1. Several guys said, First I have to seperate this rest-api + websocket application. Is it weird running whole application on a single server (logically)?

  2. How to unit-testing websocket code?

Thank you

like image 742
whiteUnicorn Avatar asked Jul 07 '16 02:07

whiteUnicorn


People also ask

Does Socket.IO work with WebSocket?

Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.

Does Flask support WebSockets?

Flask, being a minimalist web framework, does not have WebSocket support built-in. The old Flask-Sockets extension, which has not been maintained in the last few years, provided support for this protocol.


1 Answers

Some people prefer separate apps for HTTP and WebSocket, but that is not something I would recommend blindly. There are cases in which it makes sense to do so, others that do not, specially if you are using Flask-SocketIO, which gives you a fairly powerful integration.

The Flask-SocketIO extension includes a test client, similar in concept to Flask's test client, but meant to be used with socket routes. Have you looked at that?

If you want an example application that uses Flask, Celery, Socket.IO and has a nice set of unit tests for everything, have a look at Flack: https://github.com/miguelgrinberg/flack. I gave a tutorial session at PyCon 2016 in which I cover this application in detail: https://www.youtube.com/watch?v=tdIIJuPh3SI.

like image 92
Miguel Avatar answered Nov 14 '22 20:11

Miguel