Reputation: 858
I am in a situation where I am using a tcp connection on back end to stream data from SERVER A to SERVER B. SERVER A is running a client to talk to a database. SERVER B is running web server to handle requests from client. SEVER B has requirement to stream data from SERVER A to CLEINT via web socket.
i.e. A -tcp-> B -websocket-> CLIENT
Currently I am opening a web socket on SERVER B when the CLIENT requests page at /websocket. /websocket will return html page to establish connection websocket on SERVER B. Once the connection is established I am opening a new tcp connection to SERVER A while the websocket is open, and passing data from B's tcp connection to A, through websocket connection to client.
i.e.
CLIENT -> GET /websocket -> SERVER B -tcp-> SERVER A
SERVER A -data-> SERVER B -data> CLIENT
My understanding of TCP connetions and their protocol is fairly limited. From what I can gather, a web socket exists within a TCP connetion, but transfers data via different protocol (HTTP?). There is a thorough description of how they work in this video, though a lot of it is over my head. There is one section where she describes how a "key" string is passed from the client to the server and the server magically generates an "accept" string and sends response to client.
This may be a silly question but I wondering if there is some way I can forward that key from the CLIENT, through SERVER B to SERVER A, without creating a tcp connection from SERVER B to CLIENT. Goal here is to have the CLIENT create only one connection to SERVER A when /websocket is hit.
i.e.
CLIENT -websocketKey-> SERVER B -websocketKey-> SERVER A -websocketAccept-> SERVER B -websocketAccept-> CLIENT
Thanks for your help , and please be kind in your responses as I am inexperienced in this area.
Upvotes: 1
Views: 1381
Reputation: 707148
What you are describing is a proxy or gateway function where server B works as an intermediary between server B and the client and handles the fact that server A and the client speak different protocols and that the client cannot connect directly to server A. This is perfectly doable.
How you handle the conversion of data from whatever format it is in when it comes from Server B to whatever you want to send over the webSocket is entirely application-specific and up to you. It may be that you can just send the data raw as it is from Server A or it may be that you need to clean it up in some way to make it easier for the client to understand.
I do not understand what you are talking about with the webSocketKey. A webSocket connection uses a security key as part of establishing the connection, but that should be handled entirely for you with the various webSocket libraries on the client and on Server B. You don't have to participate in that at all. You aren't making a webSocket connection all the way from the client to Server A. You are making a webSocket connection from the client to Server B and then your normal TCP connection from server B to server A. server B is playing the part of a middleman fetching data from server A and then sending it over a different connection to the client. You have two separate connections:
webSocket Your TCP connection
(proprietary protocol)
client <--> server B server B <--> server A
Upvotes: 1