#Flask WebSockets Invalid session...

89 messages · Page 1 of 1 (latest)

tall shell
#

I modified my Python Flask server to use socket.io-client and am seeing errors logged after deploy. The service remains running but the server is not responding to requests properly... Any help is much appreciated!

Invalid session 8nIFLl8Lm_WsCd_2AAAA (further occurrences of this error will be logged with level INFO)
[2023-05-15 13:02:53 +0000] [10] [ERROR] Socket error processing request.
Traceback (most recent call last):
File "/opt/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 134, in handle
self.handle_request(listener, req, client, addr)
File "/opt/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 190, in handle_request
util.reraise(*sys.exc_info())
File "/opt/venv/lib/python3.8/site-packages/gunicorn/util.py", line 625, in reraise
raise value
File "/opt/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 181, in handle_request
resp.write(item)
File "/opt/venv/lib/python3.8/site-packages/gunicorn/http/wsgi.py", line 326, in write
self.send_headers()
File "/opt/venv/lib/python3.8/site-packages/gunicorn/http/wsgi.py", line 322, in send_headers
util.write(self.sock, util.to_bytestring(header_str, "latin-1"))
File "/opt/venv/lib/python3.8/site-packages/gunicorn/util.py", line 286, in write
sock.sendall(data)
OSError: [Errno 9] Bad file descriptor

iron slateBOT
#

Project ID: a686cf21-bbfd-4ddb-9e0c-5c11cd434e88

tall shell
#

a686cf21-bbfd-4ddb-9e0c-5c11cd434e88

clear ruin
#

and it cant know the file descriptor because the socket file doesnt exist

#

websockets module works for me thats the only thing i can tell

#

also use the PORT environment variable if you didnt set it in the service settings to something else cause if you didnt, it automatically makes ssl work for you without anything and it your subdomain will work

tall shell
#

Did you have to install any package like eventlet?

tall shell
clear ruin
#

if you want the subdomain, dont set it in service env variables

#

instead

#

import os

#

and set port to os.environ['PORT']

#

it will automatically forward it to

#

and on 443 port

#

for websockets you need to use

#

wss:// tho

#

(wss:// for with ssl

#

ws:// without)

tall shell
clear ruin
#

you know

#

or something

#

thats what you type in a browser

#

the main domain is the last one

#

you need to pay for domains

#

but for subdomains you can just create a directoryon the machine that is hosting the web app(depends on your software)

tall shell
#

Since I am hosting the service on Railway, I am assuming my subdomain is
flask-production-bcb7.up.railway.app

tall shell
clear ruin
#

but it seems correct

tall shell
#

In the service setting

clear ruin
#

but for custom domain you need to use your own so dont click on that button

#

if you dont have a domain bought

tall shell
#

Oh I don't want to use custom domain yet

tall shell
clear ruin
clear ruin
#

only when you connect

tall shell
# clear ruin only when you connect

I have this in my frontend React to create the socket
const newSocket = socketIOClient(process.env.REACT_APP_SERVER_URL, {
query: { token }
});
setSocket(newSocket);

#

and the env variable is just the https:// subdomain

#

I was able to talk to my server fine until I added websocket

clear ruin
#

@rocky light can probably help you better if he has time

#

i am not so much into web stuff, i never used react or web frameworks, dont ask me

clear ruin
# rocky light she*

i checked your twitch and you have a male voice, are you transgender or smth? am i wrong?

rocky light
rocky light
tall shell
#

Thanks for helping, to both of you!

tall shell
#

So in this repo, I have a React app and Flask server

rocky light
#

okay, and what did you do before the error occurred? Started the app? went to a specific endpoint?

tall shell
#

After start_chat in ln75

rocky light
#

not seeing anything that's sticking out.

#

does this happen when you first start the app or when you connect?

tall shell
#

If I redeploy the service and do not execute this code in App.js in React, seems like things are fine
// Initialize socket connection here
const newSocket = socketIOClient(process.env.REACT_APP_SERVER_URL, {
query: { token }
});
setSocket(newSocket);

#

Once I create the Socket, server starts to log the error message

rocky light
#

did you mean to use socketio and not regular websockets?

#

theyre not the same thing

tall shell
#

Hmmm. I am totally new to this...

rocky light
#

yeah theyre not the same thing, that's likely the issue here.

#

You cannot just use a socketio client to connect to a standard websocket server.

tall shell
#

But I think both the React and Flask server are socket.io?

#

Could you help me check on the main.py script

rocky light
#

hmm, okay you are using flask_socketio for the server so that might not be the issue.

#

personally id use actual websockets instead as socketio adds a whole layer of unknowns

#

sorry i couldnt be of more help

tall shell
#

ok. Let me try switch
So websocket might be more reliable?

clear ruin
#

yes, it is a lot better

rocky light
#

yeah, the issue itself is the socket is not connected and you're trying to send it data.

clear ruin
#

it handles pings by default

#

and stuff

#

its very good and nice

tall shell
#

I see. I would try that!

clear ruin
#

and very easy to set up

tall shell
#

Thank you both so much!

clear ruin
#

this will help