2

I'm trying to receive pushes from the server as a client; using my test client as follows:

Client:

socket_client = socketio.test_client(app)
@socketio.on('hit_client')
def recieve_message(json_data):
    print("Server has called!")

Server:

socketio.emit('hit_client', 'Hi Client!')

The server should be pushing and calling the hit_client channel, but that isn't being fired. However, the socket_client.get_received() has the emitted data. I thought the whole point of WebSockets was bidirectional communication (i.e. pushing function triggers)!

This is a very simple setup and it doesn't even seem to be working... Any help would be EXTREMELY appreciated. I've been slamming my head for hours.

1 Answers1

2

The test client is not a Socket.IO client. It's only purpose is to help you write unit tests for your Socket.IO server. It is similar in concept to the Flask's test client for HTTP routes. It only makes sense to use it in unit tests.

When the server emits something to the client, the test client will just store it and make it accessible in your test code via the get_received call. It will not fire any events, since that is not its intended purpose.

If you want to implement a Socket.IO client in python, there is a package for that: https://pypi.python.org/pypi/socketIO-client. With this package, you can write a Python script that connects to the Socket.IO server and can send and receive events.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152