Handle websockets in a plugin

Hi everyone!

I’ve been working in this plugin for a while: GitHub - mliezun/caddy-snake: Caddy plugin to serve Python apps

Currently I’m trying to integrate it with websockets, I need to be able to handle each incoming and outcoming message (except ping-pong).

My idea until now was to use gorilla/websocket to hijack the underlying connection.

Is there a better way to do this?

1 Like

You can read through Caddy’s reverseproxy package source, specifically the streaming.go file. That’s where we do the WebSockets handling.

I wonder if it would be possible for you to implement your Python stuff as a reverse_proxy transport instead of as its own handler directive. That way you could inherit all this functionality from the proxy handler and just forward the request where it needs to go with the protocol of your choice. That’s how php_fastcgi is implemented, which is a shortcut for a reverse_proxy handler with a fastcgi transport.

3 Likes

Thank you for your suggestion!

I like the idea of implementing the Python stuff as a new transport.

I’ve been taking a look at how reverseproxy and streaming.go are implemented.

One thing that is not clear to me is how could I modify the stream processing to catch websockets messages. Take in consideration that according to ASGI Spec I need to identify ws messages when interacting with a Python app.

Is it not just a TCP pipe to the Python upstream? That’s what websockets is, it’s HTTP to start, then TCP afterwards.

I don’t know anything about Python transports, so I don’t think I can help much.

1 Like