Forums

Websocket error

Hello! I've been developing a new part of my website and have managed to get it pretty much working in my debug environment. Tonight was the first go I've had at testing it live on PythonAnywhere. Needless to say, there were a lot of things I haven't got right yet! I can see my way through most of these, but the biggest problem seemed to be that I'd used websockets:

socketio.emit( "My channel", {"reload": True} )

in the back-end. Then, I intercept this in the front end with:

<script src="https://cdn.socket.io/4.7.5/socket.io.min.js"></script>

socket.on(...

To cut a long story short, this works fine in my development environment, but only works sporadically at best (possibly not at all) when hosted from PythonAnywhere. The upshot is that the new bit of the site is painfully slow. I disabled all the websockets code and it was much faster once I'd reloaded the website.

I did a search of the forum and found that websockets are not yet properly supported on PythonAnywhere. However, it is mentioned that 'outbound' websockets work. I don't know enough about any of this stuff to know whether my application qualifies as an 'outbound websocket' or not, so I have a few questions. Before I ask those, let me give a bit of background by explaining that the only reason I'm using these is that I might have multiple users looking at the same page and I want to trigger a browser update for all users if one of them makes a change. This might be the programming equivalent of using a sledgehammer to crack a nut. I don't really know what I'm doing, so this may well be the case.

Anyway, I'd really like answers to the following questions, and particularly any advice that anyone can offer:

  1. Is what I'm trying to do (with websockets) a complete non-starter on PythonAnywhere?
  2. If the answer to the first question is 'no', then any ideas what I might be doing wrong?
  3. If the answer to the first question is 'yes', what's the easiest way to achieve the same effect (firing browser refresh events from the server) without using websockets?

Any help is greatly appreciated! I'm practically a beginner with most of this stuff, so feel free to point out what might seem obvious.

You'll have to use an ASGI web app if you're using socketio. It's still in beta, but we have instructions on how to get started via API here: https://help.pythonanywhere.com/pages/ASGICommandLine If you would like us to also enable the (still limited) UI for this, please email us at support@pythonanywhere.com.

Thanks for the links. That looks really interesting, but unfortunately there are several things in the list of caveats (e.g. only https on the default subdomain) that mean it won't work for me on this occasion.

If anything springs to mind, then please could you suggest a PythonAnywhere compatible workaround to achieve the same effect (i.e. triggering a browser refresh for all users when one user submits data to the server)? I'm open to any suggestions, even hacky ones.

How often/quickly do you need the data to refresh?

It's probably not needed very often (it's likely to be several minutes between updates being needed), but it would be nice if the resultant refresh event was received by users within a few tens of seconds at most (preferably faster). It certainly doesn't have to be responding instantly, though.

You could still contact us at support@pythonanywhere.com and we can discuss what can be done in your case with the features that we are working on at the moment.

Thanks. I'll see how far I can get myself and get in touch if I can't figure out a satisfactory workaround.

All right. See you there.