Python Websocket Server (original) (raw)
Hi,
first of all, my basic idea: I would like to program an Android app that sends the current GPS to a server every second, for example. The server should receive the GPS from all clients and the GPS coordinates should be displayed on a map. In addition, a few calculations are performed on the server and data is reported back to the clients.
I don’t have a lot of experience yet and have therefore done some research, but there aren’t many articles on this.
My idea would be to program the server as a websocket server in Python.
Is it then possible to start the Python program on a Linux Vserver from Strato, for example?
And how does the visualization work? Can this also be done on the server or would you need, for example, a “master client” that receives all GPS coordinates from the other clients and then displays them on a map and the “master client” runs on my local Windows PC, for example.
And I don’t want to run everything on my local Windows PC, as this should of course work continuously and a few calculations should also be carried out with the GPS coordinates and some data should also be reported back to the clients.
However, the UI does not have to be active all the time, it is just a bonus.
Or should the task be approached completely differently? Does anyone have any ideas?
Thanks!
elis.byberi (Elis Byberi) May 4, 2025, 1:25pm 2
I’d start with an HTTP server (a.k.a. a web server). After that, I’d begin to focus on GPS, and finally, the GUI. Then, perhaps I’d start thinking about WebSockets.
Do you have a web server running yet? Something like Flask or Bottle?
Rosuav (Chris Angelico) May 4, 2025, 1:45pm 3
That’s definitely possible!
That specifically, though, I have no idea about. I tried to search up Strato and found a somewhat broken strato.de server, and then this page, which suggests that Strato may have been bought out by a larger provider. Regardless, though, if it’s a server that lets you run whatever you need, you can definitely run a web server and host websocket connections. (That may or may not be true for other types of hosting, but websockets are fairly common, so the odds are in your favour.)
It’s hard to be specific without knowing exactly what visualization you want to do, but broadly speaking, if you can write it in Python, you’re good. For example, Python can take an image (eg a map) and make changes to it (eg adding markers where people are). Python can also take a series of regions and colour them (say, green regions where people are, with paler regions having fewer people and darker regions having more). So, probably yes, you would be able to!
You have two basic options here, then. One is that you do all the work on the server, and then feed the result to a web browser as an image. Pretty straight-forward, although if you need it to update rapidly, it can be a lot of traffic. The other is that your server provides a structured set of data (say, in JSON form) over a websocket, and then JavaScript in the client does the visualization. This can work really nicely with UI features like zooming, since the client can zoom the map and then place the markers as needed. It would mean that you need to learn both Python and JavaScript to make this work, but it’s well worth it for the flexibility. That said, since you’re talking websockets, you probably already know a bit of JS.
Elis says to start with an HTTP server. I agree, but I would put websockets before working on the GPS and GUI. Design your app with this in mind, and plan out how your front end and back end will talk to each other. I recommend developing an extensible protocol that everything then fits into. For an example, here’s how my Twitch bot talks to its front end:
{"cmd": "init", "type": "serverstatus", "group": ""}
{"cmd": "update", "cpu": 1, "ram": 13, "ramtotal": 31, "socket_count": 2, "spinner": "⠇"}
The client sends the first message, and then the server sends messages like the second, repeatedly. (You can see this in action at StilleBot server status - Mustard Mine if you’re curious. The back end isn’t Python, but everything else I’ve said still applies.) The client can send other messages too if it needs to,and the server will keep it up-to-date as changes happen.
To make this work on your back end, you will almost certainly want some form of asynchronous I/O - either the asyncio
module itself, or some other similar tool. Look into those and get to know them; they’ll be very helpful.
Jonas0512 (Jonas) May 4, 2025, 5:05pm 4
Okay thanks for your answer. No not yet. I thought I would ask in advance how best to solve such a task.
Jonas0512 (Jonas) May 4, 2025, 5:10pm 5
Okay thanks for your detailed answer! Is it with websocket possible to have 1000+ or 10.000+ clients or is that way too much?
Rosuav (Chris Angelico) May 4, 2025, 5:16pm 6
Absolutely! It does depend a bit on how much traffic each one generates, though. Concurrent clients don’t cost you very much; you’re going to end up saturating on CPU load or network traffic long before the raw number of connected clients begins to hurt.
The exact traffic level you can sustain will depend a lot on the hardware you’re running it on, and 1K-10K might require that you get something beefy with a good amount of RAM, but it’s definitely possible.
Jonas0512 (Jonas) May 4, 2025, 6:02pm 7
Okay, that confirms that Websockets is the right choice for the task.
I will start to try it out the next days and try to pass the GPS coordinates to a local websocket server within a WiFi.
I think the big challenge will be when the websocket app is running on a rented server.
Thanks for the help. I will definitely come back here with more specific questions.