Skip to content

Use Websocket to communicate with backend #105

@FHU-yezi

Description

@FHU-yezi

Now, when I trigger anything which bind to a callback, I can see an OPTIONS and a POST request.

My first question is, I read the code about handle network request, and found we added a middleware called add_cors, which related to CORS in web developing.

I'm new for this but in my mind, the frontend and backend is in the same domain / subdomain, and we use the same port for both of them. Maybe this middleware is not nesserary, if we remove it, and the browser not send OPTIONS request, can we save 2 RTTs or more time?

Second question, when the issue comes to frequently communicate between backend and frontend, we can use Websocket to solve this.

I found one of our developers' in Hacker News, which said there are not noticeable latency influence between Websocket and HTTP request. I don't agree with this, in my case, I noticed we are spending ~40ms on every event, on localhost.

Our website also has an experience issue related to this.

To deal with this problem, we are going to create a middleware for our frontend. It can convert event update request to Websocket request, and for response, convert them to the same form which original POST request use.

For our backend, FastAPI has native Websocket support, so it shouldn't be a big problem.

There are already similar framework, both aims to create Web Apps in pure Python, use Websocket to communicate with backend, called PyWebIO. I am already created a few Apps on it.

Take one of my projects for example, I built a webpage, user input there URL of JianShu ( just like a Chinese version of Medium ), and I use MongoDB to query there records of being on the articles rank, returns to the frontend and display them.

I bind on_change event of the input box, and when use type something, they got autocomplete. I got ~150ms type-to-display latency on Internet, that is the magic of Websocket.

Metadata

Metadata

Labels

enhancementAnything you want improved

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions