Chris
Chris

Reputation: 5073

Python websockets

Is it possible to have one protocol connect to another protocol on the same server? My goal is to accept a request for one protocol and then pass that request to a different protocol and have that second protocol return values to whatever client is connected to it.

I'm thinking you would transfer the onMessage request to the other protocol some how.

I don't have any code to show as I don't know where to start, but any code examples would be appreciated.

Upvotes: 1

Views: 628

Answers (1)

jfriend00
jfriend00

Reputation: 707996

What you're asking for sounds like a proxy server. A proxy can simply be a middleman that speaks the same protocol out both ends (as in a typical http proxy) or it can be some sort of translator that has one protocol coming in and another protocol going out.

So, supposed you want a browser to be able to use a webSocket connection to speak to some other server that doesn't speak the webSocket protocol. You could implement a proxy server yourself that allows the browser to connect to it and then, via your proxy, it could send/receive messages with the other server that speaks a different protocol.

To implement a proxy server like this, you would do the following:

  1. Create a server process that listens for incoming webSocket connections. This would allow the browser to connect to your proxy.
  2. Once connected, the browser would send a message (of your own design) over the webSocket.
  3. Your proxy would receive that message and translate it to the protocol of the other server,
  4. Your proxy would then connect to that other server and send the message to the other server.
  5. Your proxy could then receive a response from that other server and then, if needed, send a translated response message back to the browser over the webSocket.

It would be the proxy's responsibility to translate each message data from what it received over the webSocket to whatever format/protocol the other server speaks.

It would be an implementation choice whether you maintained a dedicated connection between the proxy and the other server for each webSocket connection or whether you directed all requests over one dedicated connection or whether you created a new connection upon demand only for the duration of a given request. Which makes the most sense depends entirely upon the characteristics of the other server, number of requests and the work that is being done.

Upvotes: 3

Related Questions