Flask WebSockets Invalid session...

AJA J5/15/2023
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
AJA J5/15/2023
a686cf21-bbfd-4ddb-9e0c-5c11cd434e88
Ccsdaniel5/15/2023
the problem suggests that you are sending data to a socket that doesnt exist
Ccsdaniel5/15/2023
and it cant know the file descriptor because the socket file doesnt exist
Ccsdaniel5/15/2023
websockets module works for me thats the only thing i can tell
Ccsdaniel5/15/2023
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
AJA J5/15/2023
Did you have to install any package like eventlet?
AJA J5/15/2023
Also do I need to set up the PORT environment variable? Sorry I did not get the last part..
Ccsdaniel5/15/2023
want the subdomain?
Ccsdaniel5/15/2023
no but it should still work, websockets works
Ccsdaniel5/15/2023
if you want the subdomain, dont set it in service env variables
Ccsdaniel5/15/2023
instead
Ccsdaniel5/15/2023
import os
Ccsdaniel5/15/2023
and set port to os.environ['PORT']
Ccsdaniel5/15/2023
it will automatically forward it to
Ccsdaniel5/15/2023
and on 443 port
Ccsdaniel5/15/2023
for websockets you need to use
Ccsdaniel5/15/2023
wss:// tho
Ccsdaniel5/15/2023
(wss:// for with ssl
Ccsdaniel5/15/2023
ws:// without)
AJA J5/15/2023
Sorry I am still learning about this and can't follow...
What do you mean by want the subdomain?
Ccsdaniel5/15/2023
subdomain is the part of the url
Ccsdaniel5/15/2023
you know
Ccsdaniel5/15/2023
subdomain.domain.com
Ccsdaniel5/15/2023
or something
Ccsdaniel5/15/2023
thats what you type in a browser
Ccsdaniel5/15/2023
subdomain.subdomain.domain.com
Ccsdaniel5/15/2023
the main domain is the last one
Ccsdaniel5/15/2023
you need to pay for domains
Ccsdaniel5/15/2023
but for subdomains you can just create a directoryon the machine that is hosting the web app(depends on your software)
AJA J5/15/2023
Since I am hosting the service on Railway, I am assuming my subdomain is
flask-production-bcb7.up.railway.app
AJA J5/15/2023
Is that correct?
Ccsdaniel5/15/2023
where you found that
Ccsdaniel5/15/2023
but it seems correct
AJA J5/15/2023
In the service setting
Ccsdaniel5/15/2023
change it to what you want
Ccsdaniel5/15/2023
yes
Ccsdaniel5/15/2023
but for custom domain you need to use your own so dont click on that button
Ccsdaniel5/15/2023
if you dont have a domain bought
AJA J5/15/2023
Oh I don't want to use custom domain yet
AJA J5/15/2023
and do I need to set this URL anywhere?
Ccsdaniel5/15/2023
read the websockets documentation if you want to use it
Ccsdaniel5/15/2023
no
Ccsdaniel5/15/2023
only when you connect
AJA J5/15/2023
I have this in my frontend React to create the socket
const newSocket = socketIOClient(process.env.REACT_APP_SERVER_URL, {
query: { token }
});
setSocket(newSocket);
AJA J5/15/2023
and the env variable is just the https:// subdomain
AJA J5/15/2023
I was able to talk to my server fine until I added websocket
Ccsdaniel5/15/2023
@ImLunaHey can probably help you better if he has time
Ccsdaniel5/15/2023
i am not so much into web stuff, i never used react or web frameworks, dont ask me
IImLunaHey5/15/2023
she*
Ccsdaniel5/15/2023
i checked your twitch and you have a male voice, are you transgender or smth? am i wrong?
IImLunaHey5/15/2023
doesnt matter what my voice sounds like... there are loads of cis/trans men/women who have deep/high voices. if someone corrects their pronouns just leave it at that.
Ccsdaniel5/15/2023
ok sorry, i didnt know
IImLunaHey5/15/2023
gonna have to provide more info than that. got a link to a repo or a code sample?

The errors states you're using a closed socket.
AJA J5/15/2023
Thanks for helping, to both of you!
AJA J5/15/2023
Let me post the repo here
AJA J5/15/2023
So in this repo, I have a React app and Flask server
IImLunaHey5/15/2023
okay, and what did you do before the error occurred? Started the app? went to a specific endpoint?
AJA J5/15/2023
After start_chat in ln75
AJA J5/15/2023
in main.py
IImLunaHey5/15/2023
not seeing anything that's sticking out.
IImLunaHey5/15/2023
does this happen when you first start the app or when you connect?
AJA J5/15/2023
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);
AJA J5/15/2023
Once I create the Socket, server starts to log the error message
IImLunaHey5/15/2023
did you mean to use socketio and not regular websockets?
IImLunaHey5/15/2023
theyre not the same thing
AJA J5/15/2023
Hmmm. I am totally new to this...
IImLunaHey5/15/2023
yeah theyre not the same thing, that's likely the issue here.
IImLunaHey5/15/2023
You cannot just use a socketio client to connect to a standard websocket server.
AJA J5/15/2023
But I think both the React and Flask server are socket.io?
AJA J5/15/2023
Could you help me check on the main.py script
IImLunaHey5/15/2023
hmm, okay you are using flask_socketio for the server so that might not be the issue.
IImLunaHey5/15/2023
personally id use actual websockets instead as socketio adds a whole layer of unknowns
IImLunaHey5/15/2023
sorry i couldnt be of more help
AJA J5/15/2023
ok. Let me try switch
So websocket might be more reliable?
Ccsdaniel5/15/2023
yes, it is a lot better
IImLunaHey5/15/2023
yeah, the issue itself is the socket is not connected and you're trying to send it data.
Ccsdaniel5/15/2023
it handles pings by default
Ccsdaniel5/15/2023
and stuff
Ccsdaniel5/15/2023
its very good and nice
AJA J5/15/2023
I see. I would try that!
Ccsdaniel5/15/2023
and very easy to set up
AJA J5/15/2023
Thank you both so much!
Ccsdaniel5/15/2023
this will help
Ccsdaniel5/15/2023
it has asyncio, threading, sans io support