jetty / jetty.project

Eclipse Jetty® - Web Container & Clients - supports HTTP/2, HTTP/1.1, HTTP/1.0, websocket, servlets, and more
https://eclipse.dev/jetty
Other
3.84k stars 1.91k forks source link

Does Jetty support websocket proxying? #10811

Open Spandana806 opened 11 months ago

Spandana806 commented 11 months ago

11.0.17

Question Does Jetty support websocket proxying? I want my application to be able to proxy http requests and websocket requests via Jetty server.

Using ProxyServlet.Transparent, I am able to redirect my http requests to target server and get responses. How do we achieve the same redirection for websocket subscriptions?

The below implementation works: image

Code snippet:

public class MyProxy extends ProxyServlet.Transparent
{

    @Override
    public void init(ServletConfig config) throws ServletException 
   {
        myServletConfig = buildMyServletConfig(config);
        super.init(myServletConfig);
    }
   @Override
    protected String rewriteTarget(HttpServletRequest request) 
   {        
        myServletConfig.setProxyTo("http://targetServer/endpoint");
        super.init(myServletConfig);
        return super.rewriteTarget(request);     
    }
}

How do we achieve the reverse proxying for websocket subscriptions? image

sbordet commented 11 months ago

WebSocket proxying is possible, but not implemented -- this is one of the first times we get asked about this.

We have implemented it as a proof-of-concept (not production ready code) in a test case here: https://github.com/jetty/jetty.project/blob/jetty-10.0.18/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/proxy/WebSocketProxy.java

If you want to write your own you can start from there.

Alternatively, you may establish a commercial support from Webtide, and we will write the code for you and for the whole community -- a great way to give back.

Spandana806 commented 11 months ago

Hi Simone, Thank you for the prompt response. As per the document, there are two ways of implementing websockets, I have tried the first way of using standard javax.websocket API. I have added a websocket server which opens a new client session to my target server and thus acting as a proxy between actual client and the target server.

Below is the pictorial representation of how I achieved websocket proxying using javax APIs: image

Please confirm if it is the right way to achieve websocket proxying.

I will be trying your suggestion which uses Jetty-specific WebSocket APIs. Is there any difference in terms of performance or in any other way, between the standard javax APIs and the jetty specific APIs?

sbordet commented 11 months ago

I recommend you do it with the Jetty 12 WebSocket APIs because they have better support for backpressure: https://eclipse.dev/jetty/documentation/jetty-12/programming-guide/index.html#pg-server-websocket-jetty

The problem with the Jakarta APIs is that you have to block if a remote peer is slow, and this could cause scalability issues.

With the Jetty WebSocket APIs you have full control on asynchronous writes and on the demand for more WebSocket frames.

Spandana806 commented 11 months ago

Thank you, I will try with Jetty12 APIs.