Using caddy 0.9.1 with socket.io and flask socket.io

I’ve been working with the flask-Socketio library that allows for the Python Flask web framework to be used with Socket.io. This is websocket technology. I searched the forums and also the bug reports for caddy, but I couldn’t find a complete example detailing how to set up the websocket.

I am using caddy version 0.9.1.

Using the Caddyfile listed below, the browser console (Chrome, on Windows 10) shows a number of errors related to GET (net::ERR_ADDRESS_IN_USE). Is there a complete example showing how to set up a websocket proxy?

Note that there is a complete example on how to set this up using the NGINX server (Flask-SocketIO — Flask-SocketIO documentation):

server {
    listen 80;
    server_name localhost;
    access_log /var/log/nginx/example.log;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_redirect off;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /socket.io {
        proxy_pass http://127.0.0.1:5000/socket.io;
        proxy_redirect off;
        proxy_buffering off;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }
}

Here is my Caddyfile that does not work:

localhost:8080

proxy /search localhost:5000
proxy /mappins localhost:5000

proxy /socket.io localhost:8080 {
        proxy_header Host {host}
        proxy_header X-Real-IP {remote}
        proxy_header X-Forwarded-For {remote}
        websocket
    }

The caddy server is being started from the command-line:

caddy -http2=false

Giving this another try with another Caddyfile, I get a Bad Gateway as shown by the console log:

socket.io.js:3511 GET http://localhost:8080/socket.io/?EIO=3&transport=polling&t=LQWkXFu 502 (Bad Gateway)

Here is the alternate Caddyfile:

localhost:8080

proxy /search localhost:5000
proxy /mappins localhost:5000

proxy /socket.io localhost:8080 {
        proxy_header Host {host}
        proxy_header X-Real-IP {remote}
        proxy_header X-Forwarded-For {remote}
        websocket
    }
    
proxy /socket.io localhost:5000 {
        proxy_header Host {host}
        proxy_header X-Real-IP {remote}
        proxy_header X-Forwarded-For {remote}
        websocket
    }

@n.kinar I have the same issue, do you have any update on this?

@gyufa Sorry, I don’t have an update to share on this issue. I actually had to switch to nginx, and this works fine. Too bad that caddy doesn’t work with flask-sockio.

@n.kinar I’ve spent some time playing around with this, and I’m glad to tell you that it seems to be resolved on the master branch. The related issue was https://github.com/mholt/caddy/issues/1056 and the commit that fixed it is https://github.com/mholt/caddy/pull/1062. There is no official release with this fix since 0.9.1, but you can compile a binary from source if it’s still not too late. If you use Docker, I can share an image with you.

2 Likes

@gyufa That’s great, thanks so much for letting me know! I’m not using Docker at this time, but I will pull the latest development version, compile it and then post back here.
Once again, thank you - I like caddy better than the other alternatives.

1 Like

@gyufa: I can now confirm that the most recent version of caddy works well with websockets, socket.io, and flask-socket.io. Thanks again for your help!

For the record:

I downloaded and built caddy using:

go get github.com/mholt/caddy/caddy

Here is the complete caddyfile that works well. Note how socket.io is being proxied:

localhost:8080 
 
proxy /search localhost:5000 
proxy /mappins localhost:5000 
 
proxy /socket.io localhost:5000 { 
        header_upstream Host {host} 
        header_upstream X-Real-IP {remote} 
        header_upstream X-Forwarded-For {remote} 
        websocket 
    } 

Caddy is being started as:

caddy -http2=false

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.