miguelgrinberg / Flask-SocketIO-Chat

A simple chat application that demonstrates how to structure a Flask-SocketIO application.
http://blog.miguelgrinberg.com/post/easy-websockets-with-flask-and-gevent
MIT License
673 stars 239 forks source link

How best to test WS events? #6

Closed wcpines closed 5 years ago

wcpines commented 7 years ago

Hey Miguel,

I'm playing around with building a chat app API/backend using Flask-SocketIO. However, I'm currently stuck trying to test socketio events. With standard RESTful endpoints, I can easily use curl or HTTPie. But what about testing something like joining a room? E.g.

@socketio.on('join')
def on_join(data):
    username = data['username']
    room = data['room']
    join_room(room)
    send(username + ' has entered the room.', room=room)

Am I missing something?

Thanks!

miguelgrinberg commented 7 years ago

@wcpines you have basically two options.

If you want to go the mock route, you can use the test client that comes with Flask-SocketIO. Take a look at the unit tests in that package to see how that works, but it is similar in concept to the Flask test client for HTTP routes. This option does not use a real server, so it is ideal for unit tests.

If you want to test things on a running server, then you need to use a Socket.IO client to connect to the server and send events. There is one for Python that I heard from users works well with this server: https://pypi.python.org/pypi/socketIO-client.

wcpines commented 7 years ago

OK great, thank you!

miguelgrinberg commented 5 years ago

This issue will be automatically closed due to being inactive for more than six months. Seeing that I haven't responded to your last comment, it is quite possible that I have dropped the ball on this issue and I apologize about that. If that is the case, do not take the closing of the issue personally as it is an automated process doing it, just reopen it and I'll get back to you.