In websocket how does server identify a client's webserver - websocket

If I am not wrong, to have a push technology the client ( say browser ) also needs to run a small web server which is listening on some port ( say ijetty runs on 8080 ). Now when the actual server comes to know about any event, it sends the event to client. This way there is no PULL mechanism involved at all. Is this right ? OR there is a persistent connection involved and server sends the data on that connection whenever the event happens. My question is : in the former case ( if it is true ), how does server know about client's IP ?

WebSockets working with socket based on TCP connection, basically the client make a request for connection to the server with a challenge, websocket version, ip and more data, then the server decrypts the challenge and return his result back to the client, this process called Handshake.
If the handshake is approved, the connection is made, the socket connection remains open between the client and the server, heartbeats will be sent from the server to the client like a ping to check if the connection is still open.
read this wiki to find out more:
http://en.wikipedia.org/wiki/WebSocket

Related

Firewall blocks connection to second WebSocket server

In short we have two separate servers for our web app. The first one is the main server that uses Websockets for handling "chat rooms", and the second server only handles WebRTC audio chat rooms via Websocket. Both servers use Express to create a HTTPS server, use secure Websocket and the port 443.
I recently encountered a problem where a corporate client's firewall blocked the wss-connection to only the WebRTC server. The error logged in the user's browser was "ERR_CONNECTION_TIMED_OUT", which means the user never connects via Websocket. This has not happened with any other clients.
The Websocket connection works normally between the user and the main server, and no rules have been added to their firewall to use our app.
Has anyone encountered something similar? What kind of a firewall setting might cause this? Could this be a cors problem, since the servers are on their own sub-domains?
The main server could be restricting the type of data sent on port 443, which will use SSL to secure that transmitted data.
Refer to this page for information on the "Well-know port numbers".
The WebRTC audio data may need to be transmitted on its own dedicated port number that has been configured on the main server for this.
The problem was that the main server WebSocket used TCP and the WebRTC server used UDP, and UDP was blocked by corporate firewall on default.
WebRTC should use TCP as a backup, but I'm assuming UDP is still needed for the handshake.

ListenSocket ClientSocket Go

In this article about Go Web Server, there're Listen Socket and Client Socket in Go,
I can't understand why GoLang need two sockets Listen Socket, Client Socket but not just one socket, can anyone explain its concept or give a metaphor?
EDIT : I update my answer.
Maybe I misunderstanding the graph or the graph isn't draw very well, possibly Listen Socket, Client Socket are same socket, if the socket hasn't accept connection from client, it's called Listen Socket, and after it accept the connection, it's renamed to Client Socket, there's only one socket with different stage and name.
UPDATE 1:
I find a better article and graph about Socket Working Here.
In the grpah of article, it's clear when there's new connection, TCP Server will
create a new socket to handle the connection, and the Listen Socket continuing listening for other connections.
Here's a paragraph in the article:
The first socket created by a TCP server, via NetSock_Open(), is typically designated a listen socket , and, after the call to NetSock_Listen(), remains open indefinitely, to allow the server to respond to various connection requests. Rather than using this socket to exchange data with requesting clients, the server will create a new socket for each request.
UPDATE 2
Since first update is working on Micrium, I find another seems more general TCP working instuction Here:
TCP connection flow The following sequence shows the flow of a TCP
connection:
The server creates the listener socket that is waiting for remote clients to connect.
The client issues the connect() socket function to start the TCP handshake (SYN, SYN/ACK, ACK). The server issues the accept() socket
function to accept the connection request.
The client and server issue the read() and write() socket functions to exchange data over the socket.
Note: There are several SSL APIs that you can use to send and receive data other than the read() and write() socket functions.
Either the server or the client decides to close the socket. This causes the TCP closure sequence (FINs and ACKs) to occur.
The server either closes the listener socket or repeats beginning with step 2 to accept another connection from a remote client.
Note: Normally after the accept() socket function ends, the server divides
into two processes (or threads). The first process handles the
connection with the client and the second process issues the next
accept() socket function. Figure 1 shows an example of a TCP
connection:
Note:
I find another Socker Programming Tutorial mention about working detail in TCP.
And In .NET Framework MSDN, the explanation about Socket.Accept Method() says Accept synchronously extracts the first pending connection request from the connection request queue of the listening socket, and then creates and returns a new Socket.
I have skimmed RFC about TCP before Update 1, but I didn't see it mention the detail that Listen use one socket, and when Accept it'll create another new Socket.
Maybe the thorough way is to research the Source Code about Create Socket and Connection in Go, but I'm not consiedring to do it now.

Requests from server to client

I know already about the web-sockets, and they are great, the problem with them is that they have to keep the connection open in order to be able to communicate.
I have a small system where from time to time the server has to update the status and notify the clients about that, and keeping the connection open from every client is not so optimal. At same time is very important that the update on the client side to be made just in time.
So my question is, if the server has a unique address does the client have a public temporary address where the server can send request? So when the client will connect to the server it will provide it's unique address and the server will cache it, and when there will be an update the server will send the request to that address?
I understand that there many problems as the address will constantly change, but this is already other question.
If client does not have a dedicated IP-address then it is not available from WAN unless it has an open connection with any node in it.
When client from local network sends request to a server it's (client's) router remembers client's local IP-address and port and translates it using NAT protocol to one of router's free ports and then sends data further with router's own 'IP-address of the sender' in IP protocol header and 'Sender's port' in TCP header. When router get's server's response it uses NAT table from it's memory to translate addresses back and deliver data to the client. Addresses are normally kept in NAT table while connection between server and client is open. So if there are no opened connections between server and local network client then server will not be able to connect with client because server does not know how to reach it.
You say you have a small system. Why then do you think that you will not have enough free ports at your server to work with websockets? If you just want to get updates from the server (not to both send and get data through a persistently opened connection) you'll probably find long polling or SSE more suitable. It is definitely easier to implement than websockets.

Why browsers can receive incoming connections and other soft's cant!?

my question is simple
When you send data through TCP/IP protocol with EX:firefox you can receive reply on some random port that the browser listen on, while when i try to use a port for another task like CS Gaming or anything else it don't work unless i use kind of VPN ?
PS: there r no firewall blocking connection and port forwarding from my router didn't work as well.
Browsers are client apps that make outbound connections to web servers. When connecting to a server through a router’s NAT, the NAT takes note of the source and destination IP/port pairs so messages sent back from the server on the same connection are automatically routed to the correct client IP/port.
Browsers also support the websocket protocol. This feature makes it seem like the browser is listening on a specific port. However, in reality, it is initiated on a new connection to the server, a connection which remains open all throughout the websocket communication.
What matters is which peer is behind the NAT — the server or the client. For an outbound connection from a client, it can usually use any random port that is available at the time. For an inbound connection to a server, the server's IP/port must be known ahead of time and be routable. If the server is behind a NAT, the router(s) must be configured to make the server reachable from the other side of the NAT.
The server software can make a UPnP request to ask a router to forward inbound packets to the correct IP/Port. The router, depending on its configuration, may or may not honor such a request. If not, the router has to be configured manually by a network administrator.

How do i know if connection is alive with websockets?

I have a webapp, which is running in a browser. That webapp is connected to a server, which uses websockets. So the communication between the server and my client/browser is based on websockets. If some magic event occurs on the server, some webservice sends a new XML / JSON to my webapp and the new data gets displayed.
But how do i, as the client / browser, know if the connection is stil alive? Lets say i do not get any new XML for about 30 seconds. How would i know if the connection is closed/broken/server offline or everything is fine, but on the server himself no new magic event occured.
A websocket connection object has a readyState field which will tell you if the connection is still active (from the dart documentation). The readyState can be either
0 - connection not yet established
1 - conncetion established
2 - in closing handshake
3 - connection closed or could not open
You can also define an event handler for the websocket close event if this is something you'd like to handle (try to reconnect, etc).
3 ways:
rely on TCP to detect loss of connectivity, which will ultimately pop up in JS onclose event
send WebSocket pings from server .. browsers will reply with WS pongs, loss of connectivity is probably more robustly detected also on client side
send app level heartbeats from browser to server, server need to have logic to reply. you can't trigger WS pings from browsers (in JS)

Resources