How To Use WebSocket With FastAPI

And create real-time applications.

Fernando Souza
Vacatronics

--

Photo by Markus Winkler on Unsplash.

The Hypertext Transfer Protocol (or HTTP) is one the most used protocols on the internet today. It allows a client to fetch a resource, such as HTML pages and images. This article you are reading now was transmitted via HTTP (actually, its secured form, HTTPS).

The client (usually a browser) requests a resource (an image, a CSS file, etc) to the server, and the server responds with the requested data. It is a strictly unidirectional protocol. A server will only send data when requested by a client.

Source: Mozilla.

Open a connection, request what you want, wait for the response, and close the connection. That is the basic of HTTP. It works fine when you are dealing with documents. But when it comes to more “realtime” applications, such as gaming and chats, you start to have some problems with this approach.

Since there is no way to the server to send the client a message when a resource has changed, the client will have to continuously ask the server for the resource data. It wastes resources, as a new connection is created for everything request, even though nothing might have changed. This method is…

--

--

Fernando Souza
Vacatronics

Enthusiast of programming, electronics, technology and beer, not necessarily in that order. BuyMeACoffee: buymeacoffee.com/ustropo