Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask-SocketIO returns [] on get_received function call #2063

Closed
shivansh-vishwakarma opened this issue May 6, 2024 · 10 comments
Closed

Flask-SocketIO returns [] on get_received function call #2063

shivansh-vishwakarma opened this issue May 6, 2024 · 10 comments
Assignees
Labels

Comments

@shivansh-vishwakarma
Copy link

Flask-SocketIO's get_received(namespace=None) function is not returning the messages received from server.
I am using SocketIOTestClient for writing test cases for the flask application.
Went through the code seems like the get_received uses self.queue to get the events to emitted and couldn't find where the queue is being populated. Can someone help here?

@miguelgrinberg
Copy link
Owner

@shivansh-vishwakarma Please provide a complete and runnable example test that shows the problem and I'll investigate.

@shivansh-vishwakarma
Copy link
Author

shivansh-vishwakarma commented May 6, 2024

Hope this helps.
Do let me know if you need anything that'll help you in the investigation

class TestSocketEmit(TestCase):
    def setUp(self):
        self.client = app.test_client()
        self.api_key = "API_KEY"
        self.socketio_test_client = SocketIOTestClient(app, socketio=socketio, headers={'x-api-key': self.api_key})
        self.socketio_test_client.connect(headers={'x-api-key': self.api_key})

    def test_websocket_emit(self):
        self.socketio_test_client.emit('your_event', {'data': 'test'})
        response = self.socketio_test_client.get_received()

    def tearDown(self):
        self.socketio_test_client.disconnect()

In the given code the response is[]

@miguelgrinberg
Copy link
Owner

How is this any different than the many tests in this repository's test suite? I wanted to see a complete and runnable example, because this feels like it is a problem in your own setup, not in this package. Please provide a complete example that includes the application to be tested.

@shivansh-vishwakarma
Copy link
Author

@miguelgrinberg This should suffice the requirement. I've abstracted out the functionality of flask and socket to reproduce the same with minimal code.
If we can make it work here, it'll work in my project as well.

https://github.com/shivansh-vishwakarma/flask-socket-scratch

@shivansh-vishwakarma
Copy link
Author

One more thing that i noticed which abstracting the functionality was,
If we emit an event to the main application server in the src code using SocketIO.emit() Then i get response in the get_received() method. but while doing it with SocketIOTestClient.emit() it doesn't work out.
Might be helpful in the investigation.

@miguelgrinberg
Copy link
Owner

Okay, thanks. This is actually a different bug in Flask-SocketIO, I will have a fix in the next couple of days.

@miguelgrinberg miguelgrinberg self-assigned this May 6, 2024
@shivansh-vishwakarma
Copy link
Author

shivansh-vishwakarma commented May 7, 2024

Also, Can you confirm if there is support to emit messages to a particular room using SocketIOTestClient and receive the messages from a particular room using get_received or some other function. I tried but couldn't find it.

@miguelgrinberg
Copy link
Owner

If the client is in the room, then any messages sent to that client should appear in the test client's get_received. If the client is not in the room, then the messages will not be sent to that client or test client.

@miguelgrinberg
Copy link
Owner

@shivansh-vishwakarma in the example you provided, your test sends this event:

self.socketio_test_client.emit('your_event', {'data': 'test'})

But your server does not have an event handler for your_event. What was the expected outcome in this case? As far as I can see, your test is incorrect. You are sending an unknown event to the server, so the test client gets no reply. I added this missing event handler and test started to pass:

socketio.on('your_event')
def your_event(data):
    emit('got it')

Am I missing anything?

@shivansh-vishwakarma
Copy link
Author

shivansh-vishwakarma commented May 14, 2024

No, your are correct. My bad @miguelgrinberg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants