Websocket/REST API - which of them for a large request scenario?

208 Views Asked by At

I know that they are two different things and technical approaches, but consider this scenario: i have a third party application that will interface with my own application and every 5 minutes has to push to my services a large JSON (about 3/4MB).

My application consists in a Python-Django web application with Gunicorn serving WSGI requests and DAPHNE serving ASGI connections, in front of that i've placed a NGINX as reverse proxy.

In the web application i've created basically two endpoints:

  • REST Insert API endpoint
  • Websocket endpoint that exposes a receiver (in order to insert data

In my test environment, i've seen that when i try to push the JSON to the websocket endpoint, many times the websockets will get closed with this error message:

"HTTP_UPDATE_FAILED Error (-104): Wrong HTTP Code"

The WS endpoint in NGINX is configured as below:

 location /ws/ {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host;
    proxy_connect_timeout 15s;
    proxy_send_timeout 3600s; # ws will open for 1 hour
    proxy_read_timeout 3600s;
    proxy_buffers 512 256M;
    proxy_buffer_size 256M;

    proxy_pass http://daphne_server; #DNS resolved by internal docker dns server
  }

In order to circumvent the problem, i've tried to reduce the websocket message payload many times up to 1MB circa, and then works without any issue or errors...maybe i have to dig around the configuration of DAPHNE in order to make it working?

Using the REST api everything works flawlessly, no errors and no warnings.

I'm confused on which technology i have to point for this specific scenario: what you suggest to use for this "large" message payloads? REST API's or Websockets?

0

There are 0 best solutions below