Forums

I have a WSGI Django webapp how to upgrade it to ASGI app?

Hi everyone so I built a Django webapp as WSGI now I want to make rooms for my new PvP chess section in the app where I make rooms thru web sockets for users to create rooms and play chess. I am using Upstash Redis servers to connect the players thru the sockets. Now I understand that only ASGI apps have sockets function but when I direct the WSGI to use the ASGI then I get server error. My task is running on Uvicorn and is successful but I cannot manage to get the sockets created. If I direct the task to connect with ASGI still there is error on the browser console saying sockets not created. Has anyone managed to make such a app functional on python anywhere using any other tech. Please help me out if you have any knowledge how to get my online rooms created for my chess PvP game app. Thank you very much Lord Firedragon

Here is the documentation for how to create an ASGI app on PythonAnywhere: https://help.pythonanywhere.com/pages/ASGICommandLine/

Hi Glenn I went thru the help article it is about creating a new ASGI app but I have my whole project as WSGI. Now do I need to recreate the whole project or is there a way around to just convert the WSGI to ASGI. Another thing my always on task is running well with no errors using Uvicorn. All everything ok if I could get access to Nginx file to do some modifications then I can bet it will work out fine. Everything else is set only I am getting error on the handshake of creating new websockets which I think can be solved only if I personally modify the Nginx to go along with my project. I know many hosting site allow users to personally modify the Nginx to comply with user projects. Can we get that on PythonAnywhere too. I am sure it work out then. If not that then I guess I have to halt my app feature of chess game pvp for some time.

You don't need to rewrite whole project in ASGI, you could just separate the part that needs websockets as a separate web app. Regarding the handshake issue -- you could try debugging it to see what is the exact error. E.g. is it possible that you use wrong protocol (http(s) instead of ws(s))?

chesspvp.js:16 WebSocket connection to 'wss://www.lordfiredragon.com/ws/chess/b913706e/' failed: Error during WebSocket handshake: Unexpected response code: 404<br /> see the error code I am ready to share the codes I needed ask for any part of it. I just want the app to work that's all. cd /home/LordFiredragon/LordFiredragon/lordfiredragon && DJANGO_SETTINGS_MODULE=lordfiredragon.settings.production /home/LordFiredragon/LordFiredragon/env/bin/python -m uvicorn lordfiredragon.asgi:application --uds /home/LordFiredragon/LordFiredragon/uvicorn.sock --log-level debug see the task command it is directed to asgi app. The task command working fine with success on logs. Now even If I make a separate app for this section can I bind the 2 app to the same domain or can I bind a app to subdomains as I will need it as a part of my site in games section under www.lordfiredragon.com . I think that wont be possible will it. I took help from Google and Co pilot on this both said I need to alter or adjust the nginx server file then it will work. Please give ideas if any .

OK thank you very much guys I found a alt way to get my chess rooms working using Ably for now discarded all that Uvicorn and Redis Upstash my game working fine thank you all very much

Great to hear that!