Websocket response from another server - javascript

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?

Related

Nodejs websocket client, receive multiple messages

Building on my problem (Nodejs websockets client pending promise), it appears that promises cannot be used in the conventional way with websockets.
My scenario is:
a nodejs client to open a connection to websocket server
when a 'hello from server' is received from the server, nodejs client is to respond with a 'hello from client'
server will then stream multiple messages to client
server will end stream with 'goodbye client'
client disconnects
The most promising direction I've found is https://www.npmjs.com/package/websocket-as-promised however this only handles a finite number of responses using .then. My server will respond with a variable number of responses.

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).

Can you make existing Tcp connection a Websocket to Client

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

WebSocket client and WinSock2 server, is it possible?

I have a c++ server using WinSock2 sockets,
can I connect to this server using JavaScript with WebSocket (as a client), or do I have to use WebSocket in both server and client? how can i do that?
can I connect to this server using JavaScript with WebSocket (as a client),
Yes.
or do I have to use WebSocket in both server and client?
No.
Sockets are just implementing an access layer to the IP stack.
How these are implemented at client or server side is irrelevant for establishing and using a (TCP/)IP connection.

Using Socket.io client to connect to a different socket server

I have a simple server written in C. Its capable of receiving (and sending) messages via a specified socket. I'd like to use socket.io client to send messages to this server. I setup a simple html page and tried connecting via
var socket = io('http://my.server.ip:8080');
My server gets the connection but then socket.io gives this error repeatedly on the javascript console.
GET http://my.server.ip:8080/socket.io/?EIO=3&transport=polling&t=1431027762284-4 net::ERR_CONNECTION_RESET
I'm guessing my server should do a websocket 'handshake' to establish the connection. Is there a way to transmit a message in to a socket via JS with no handshake?
Please note my server does not use any standard, Its a rudimentary socket server that can receive and respond.
There are web socket server libraries you can use.
a plain socket does not work with web sockets.

Categories

Resources