Forums

Client unable to send messages flask-socketio

I am making a texting thing here, and the client is receiving socketio messages from the server. I know this because otherwise the section where it shows messages would be empty, and it isn't.

The problem I am having is that the client can't appear to send anything to the server, after a while of waiting Chrome just said 400 bad request, and the error log wrote "OSError: write error". I did not leave the site at all, and I have heard that pythonanywhere doesn't support websockets, but the client seems to have received.

This worked perfectly on localhost, why is this not working on pythonanywhere?

We don't support WebSockets, but I believe that SocketIO will fail back to using a regular HTTP/HTTPS-based transport if WebSockets aren't available. If you look at the response body for the failing requests, then that should help work out what is causing the issue.

What is the response body?

take a look at the full response / read what is being sent back to you

I'll send you a screen shot.

Here is a screenshot:

From what I can see: The request that triggers the string of 400s is one that has a update_server entry in the payload, so either it's your code's handling of the update_server payload, or there's some sort of mismatch between the socketio javascript in the browser and the socketio library on the server.

But it worked perfectly on localhost... I sent tons of messages and they showed up just fine

Then there's some difference between your local install and your PythonAnywhere one. Use the browser developer tools to dig in to what is different and use that as a starting point to debug it.

I use virtualenv though... It should be the same with a virtualenv

I use python 3.7 for both

Oops wrong account that was still me tho

And I use a virtualenv on localhost too

if you take a longer look at your developer tools network tab screenshot, you will see that there are also requests that go through. (vs certain polling requests get a 400 bad request)

this does lead me to the same conclusion as glenn- that perhaps when socket drops down to polling, you are not dealing with that (not responding) correctly.

if your localhost was just using sockets instead of polling, then you would never seen this error in local.

have you checked your webapp logs?

The error log wrote "OSError: write error".

That would normally mean that a request timed out on the client side. If you check the access logs, perhaps you'll see some 404 errors there when the client-side JavaScript is trying to poll a non-existent view?

What is a non-existent view?

Do you want to see the code? I can show you the server-side socketio and the client side JS...

By a non-existent view, I simply mean a view that does not exist. If SocketIO is trying to request a URL that is not defined by your app, then you'll see a line with a 404 status code in the access log (which you can access from the "Web" page).

Oh but I have defined a bunch of @socketio.on() things... Should I write namespace='/'?

I don't know enough about SocketIO to answer that.

Did you take a closer look at those 400 errors that appear in your screenshot above? If you click on them, you can see the details of what was sent to your server, and what came back, which might also give you helpful information. I tried to visit your site to see them myself, but it looks like it's currently returning an internal server error.

details

Details /\

If you right-click on the "Name" of one of those errors, the lines that start "?EIO=", then you might be able to get more information.

Okay will send a pic

Giles - You are getting the error because session variables haven't been defined. Go to the main page, sign in with theses credentials:

Username: StackOverflow

Password: StackOverflow

Then, you can see this chat

Here Is top: Top

Here is bottom: Bottom

Thanks for the login details. So as far as I can see, a request is being made to /socket.io/ on your Flask server, and it's just responding with a 400 "bad request" message, with no content to the response that indicates why. I've followed the request through our server logs, and it definitely looks like your Flask app that is returning it -- that is, the request is sent to your Flask code, and that code is returning the 400 error.

If the /socket.io view is something you've written (which I suspect it's not -- do you have an @app.route("/socket.io") in your code?) then you need to look at your code and work out why it's returning that status code. However, if (as seems more likely) it's something that's being provided by the SocketIO library you're using, you should find out where it's logging information about why it's rejecting requests. One possibility -- try adding this

app.config["DEBUG"] = True

....just after you create your Flask object in your code, and then try accessing the chat again -- you may find something useful either in the error log, or in the "Response" tab of the debug tools (near the top right of your last screenshot).

I don't not have an @app.route('/socket.io') anywhere. I think it is provided by the flask-socketio library. What will happen if I do app.config["DEBUG"] = True? Is this the same as writing debug=True in the app.run() statement? I will try it.

I did app.config["DEBUG"] = True, I didn't notice a difference. Same old errors. Any other techniques?

I'm going to try installing eventlet, I heard that is recommended

Didn't make a difference

I looked in the server log and it said the pipe was broken!!! What does this mean?

That means that the client (that is, the browser) disconnected from the server before the response was fully sent back to the client.

I think I found some information, is it still accurate?

Oh- that is still accurate. Sorry for the misleading answers on this post, flask-socketio would not work with PythonAnywhere.

Okay Guess I'm using Heroku

Loved pythonanywhere, still will use for non-socketio things, it is so much easier

Okay my website is here

thanks for the kind words! hope we can support socketio soon!

I cannot feed socket.io when i send the message the url come with '?' "https://sushantaweb.pythonanywhere.com/join?"

Websockets are not supported on PythonAnywhere. The empty query string in the URL is probably not the problem.

Is Socket.io supported now?

No, it is not.