Can you make existing Tcp connection a Websocket to Client - javascript

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.

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.
Client makes webSocket connection to Server B.
Server B accepts that connection.
Server B connects to Server A and requests data over TCP (using some proprietary protocol).
Server B starts receiving data from Server A. For each meaningful "chunk" of data it receives from Server A, it sends that data over the webSocket to the client, making sure it is in a format that the client understands.
Client starts receiving chunks of data.
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

Related

Websocket response from another server

I have a client (html and javascript), a server (python), and a separate job (python).
Can the client start a websocket connection with the server, and receive a websocket response from the job?

Can server send client data without client send request?

Normally the server-client communication works like this: the client sends some request (GET, DELETE, PATCH, etc) and the server responds client with some data. Is it possible to achieve that server sends the data to the client without the client sending request first?
To be more specific, I am wanting to build a chat room app. So when A sends a message to B, A will send a request to the server (together with that message), but B does know about this hence B does not want to request all of B's messages from the server. So how can B get notified in this case?
Without any information at all having been sent recently, this would only be possible if there's a reliable direct path to the client, and the client is also a server that can listen to requests. This approach will probably only work for clients with their own stable static IP addresses, and not from the average internet customer behind carrier-grade NAT.
But if the client can make an initial connection to the server when the client's app comes online - which is extremely typical of apps similar to what you're describing - you can set up a websocket connection at that point, allowing both the client and server to send information to each other.
Create a websocket connection to the server when the client comes online. Then, when the server sees a message change that it wants to communicate to clients, the server can iterate over all still-active websockets (or, if desired, only the still-active websockets that also need to update in response to the message), and send a message through each of them, and each connected client can see the message from the server.

How to connect HTML WebSocket to C++ Socket

I have an app where I am trying to connect to a C++ server which opens up a socket.
// Client side
var ws = new WebSocket('ws://<<IP:PORT>>');
In the server side, it is opening up a socket using
int sockfd = socket(domain, type, protocol)
(ref: https://www.geeksforgeeks.org/socket-programming-cc/)
Not aware of the c++ server implementation.
I was told that the server would start a WebSocket server to which my HTML app can connect.
But, the connection is not happening. I feel that the WebSocket on the client side is not same as socket on the server side and hence the connection is not establishing.
Please, someone, suggest what is wrong and how to make it right.
P.S. Please don't mind if it is a dumb question.
Since the server guys are not ready to change their implementation of the plain socket, as a workaround I created a C++ client which connects to the server using the plain socket. Then I am creating a WebSocket Server using NodeJs which executes the C++ client as a child process to get the data. So, my WebSocket client is now able to get the data from the main server through the NodeJs WebSocket interface server.
Cons:
A dependency of the C++ client.
Latency (since network speed is not a factor now, it works).

Web socket client JavaScript?

i created TCP Client Using Web Socket 'JavaScript' and it can connect to C# TCP Server but i can't replay in C# to handshaking sent by JavaScript TCP Client
Please Help !
thanks.
There's no way we can help you here in any specific way without seeing your code on both client and server. webSocket is a protocol, not just a plain TCP connection. For your C# server to successfully accept a webSocket connection from any client, it must follow the entire webSocket protocol.
This reference Writing WebSocket Servers provides a pretty good summary of what a server has to do in order to successfully "speak" webSocket. Unless this is merely a learning exercise for you, you will probably want to get a library/class for C# that already implements a webSocket server as I'm sure there are many.
To give you a general idea, all webSocket connections start with an HTTP request that includes an "upgrade" header, a security key and a version. If the server agrees to the upgrade, then it responds with a security key. At that point, the two sides switch from HTTP to the webSocket protocol on that same socket and from then on, all data is then sent using the webSocket Frame format. This information is all outlined in the earlier MDN reference about creating webSocket servers.
Here is an article about writing webSockets servers in C#:
MDN: Writing a WebSocket server in C#

Can websockets connect and send data to another local process running on a non http port?

I'm working on a solution that requires a webpage to print raw data onto a connected printer. One option is to send the data to the printer through a process, running on the same machine as the browser. The process listens on a specific port and forwards the data from the browser to the printer.
Is it possible to send data from a webpage to another listening process using Websockets?
Websockets can send to any port, not just port 80. But unfortunately Websockets don't transport raw data. Before the connection is established, a Websocket handshake is performed, which also includes a 32-bit masking key which is XORed with the data stream from the client. That data-stream is also framed by some additional bytes. Details can be found in the RFC.
This unfortunately means that a Websocket client can not communicate with a service which does not support the Websocket protocol. So you will need a middleware which accepts the users websocket connection, unpacks the data stream and forwards it to the printer.
One such middleware is Websockify.

Categories

Resources