Use HTTP Keep-Alive for server to communicate to client

后端 未结 3 768
-上瘾入骨i
-上瘾入骨i 2021-02-07 09:41

Recently in an interview I was asked how I would approach an online chat client application. I went through the standard \"polling\" solution but was cut off because the interv

相关标签:
3条回答
  • 2021-02-07 10:01

    With HTTP 1.1, keep-alive is the default behavior. (In HTTP 1.0, the default behavior was to close the connection.) The server must send the 'Connection: close" header to terminate the connection with the first response. So there is still a TCP socket available to push data through, but just pushing data from the server would violate the HTTP protocol in a major way. Even using keep-alive, the client would still have to poll the server.

    It is important to distinguish between HTTP Keepalive and TCP Keepalive. HTTP keepalive prevents the connection from being closed by the server or client. TCP keepalive is used when the connection might be idle for an extended period of time and might be dropped by a NAT proxy or firewall. TCP keepalive is activated on a per-socket basis by setsockopt() calls.

    When doing a 'long poll' to eliminate the need to re-poll, TCP keepalive might be needed.

    0 讨论(0)
  • 2021-02-07 10:05

    You might read more about Comet servers. That sounds basically like the approach that the interviewer was asking about. Their effectiveness is disputed by some, but it has been used in several similar situations.

    For example, I believe gmail uses comet technologies for some things (but don't quote me on it).

    Another example that seems relevant is BOSH, which is a protocol for transmitting chat information using HTTP and XMPP. But I don't believe that using keep-alive is involved in that.

    0 讨论(0)
  • 2021-02-07 10:17

    Keep-alive simply holds a TCP socket open, so each time you poll, you save the overhead of the TCP setup/teardown packets--but you still have to poll.

    However, "long polling" is a strategy for the web server to broadcast notifications to the client. Essentially, the client issues a GET request, but instead of immediately responding, the web server waits until they have a notification to send, at which point they respond to the GET request. This eliminates any need for packets to go across the wire for polling purposes, and keeps the connection stateless, which as you correctly mention is one of the purposes of the protocol.

    0 讨论(0)
提交回复
热议问题