Vanguard
Vanguard

Reputation: 1396

How to send request periodically to the client from the server by http persistent connection

I am new in http connections. The thing I want to realize is that the server should send some data (notifications) to the client periodically by persistent connection.

I wrote a code in server side by php like:

<?php

set_time_limit(0);
header('Connection: keep-alive');
$i = 0;

while($i < 10){

    echo "Hello$i<br/>";
    sleep(5);
    $i++;
}

?>

and tried to connect to the server by java:

public static void main(String[] args) throws Exception {

    URL oracle = new URL("http://localhost/connection.php");
    URLConnection yc = oracle.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            yc.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
    in.close();
}

I expected to get content from the server every five seconds like following:

Hello0<br/>
Hello1<br/>
...

but instead of this the java client is waiting 50 seconds. and printing:

Hello0<br/>Hello1<br/>Hello2<br/>Hello3<br/>Hello4<br/>Hello5<br/>Hello6<br/>Hello7<br/>Hello8<br/>Hello9<br/>

I want the server send notifications itself. instead of the client connect to the server every five seconds.

Upvotes: 2

Views: 1460

Answers (2)

Vanguard
Vanguard

Reputation: 1396

Flushing the connections seemed really good idea. But I think I found better solution. Instead of keeping connection with unlimited timeout, I think it is better to make persistent connection with 5 (N minutes) minutes timeout. It is better because when the user will be offline unexpectedly, the server will keep the connection alive anyway. and it is not good. That's why I am going to make 5 (this number is optional) connections for notification. That is the server will use first one for notification and closes the connection after sending request, and the rest 4 connections will be on duty. When the client (or java client) will receive the notification, it will make new connection to fill missing part or the connection times out. and the client will be notified immediately every time (of course if connected to the internet). If someone has better solution I will be happy to see that.

Upvotes: 0

Jerry Chin
Jerry Chin

Reputation: 667

  1. It's really unnecessary to add Connection: keep-alive header in response for HTTP/1.1 server, UNLESS for backward compatibility.
  2. No matter how long or many times you sleep in that loop, it's seen as ONE request by the client nevertheless.
  3. with that being said, your client snippet, in fact, only make ONE request to http://localhost/connection.php, and it's impossible to reuse URLConnection in order to dispatch another request(achieving persistent).

to sum up:

Persistent Connection behaviour is handled at transport layer (TCP), more specifically, you are required to reuse a client socket for multiple request to the same host plus some other requirements specified in HTTP/1.1.

Go and find some projects that are suitable for your needs, don't reinvent the wheel.

Upvotes: 0

Related Questions