Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts

    SSE: Updating content only if there is new content available?

    Is this possible in Javascript (specifically using server-side events) or would I need to update my PHP in order to display new content when its available? And correct me if I'm wrong but aren't server side events supposed to check for new updates automatically or do they just keep the connection open to allow the server-side code to do so?
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,380
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by elitis View Post
    Is this possible in Javascript (specifically using server-side events) or would I need to update my PHP in order to display new content when its available?

    And correct me if I'm wrong but aren't server side events supposed to check for new updates automatically or do they just keep the connection open to allow the server-side code to do so?

    1. both possible and needing slightly different php than normal. I recommend using PHP's APC instead of flat files or a DB to store community state. Writing a new value onto an existing event source will raise that event in javscript, which with an event handler, could display the new data that's contained in the event's arguments.


    2. no, SSE do not use long-polling (aka ping city), that's an older technique that this replaces. Since one can always instantly SEND data to a server using ajax, the missing half was being able to instantly send data from the server to the client. That requires a constant connection. This means that unlike comet, SSE is near instant in getting data from server to client.

    it's up to the server to decide when to send new data down the pipe, or a keepalive every 28 seconds. in php, you'de probably use a loop+sleep combo to poll a common data source; typically a DB, but like i said, i recommend APC since it's orders of magnitude faster than a DB.

    Node.js is nice because all the common data can just live in a variable that all requests can see, whereas PHP "boots up" each time a connection comes in, and then has to fetch the data is is availalble using closure in node.js.

    but anyways, all possible, and since EventSource is based on http, the format is simple to encode and apply to almost any back-end.
    Last edited by rnd me; 04-02-2013 at 08:33 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Can't seem to get my SSE working. It is not posting (or I guess receiving) any updates from the server.
    Code:
    if (typeof(EventSource) !== "undefined") {
    		var source = new EventSource("/ajax/gMessages.php");
    		source.addEventListener('message', function(event) {document.getElementById('chatMsgs').innerHTML+= event.data;}, false);
    	}
    PHP Code:
    session_start();
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    include 
    "/home/www/destinationx.cz.cc/includes/config.php"
    include 
    "/home/www/destinationx.cz.cc/includes/functions.php";
    $con = new PDO(DB_DSNDB_USERNAMEDB_PASSWORD);    
    $con->exec("SET CHARACTER SET utf8"); 
    $user $_POST['user']; //Not actually used or needed
    $getMsgs "SELECT * FROM `messages` WHERE (`to` = '$_SESSION[username]') AND (`from` = 'S_SESSION[username]') ORDER BY `sent` ASC";
    $receivedMsgs $con->query($getMsgs); 
    while (
    $msg $receivedMsgs->fetchAll()) { 
        
    $getUI "SELECT * FROM `users` WHERE `username` = '$msg[from]'";
        
    $uiQuery $con->query($getUI);
        while (
    $UI $uiQuery->fetch(PDO::FETCH_ASSOC)) {
            echo 
    "data: <article style='border-bottom:1px solid #FFFFFF;padding:5px;margin-bottom:2%;'>\n";
            echo 
    "data: <img style='float:left;margin:0px 3px 5px 0px;' src='{$UI['profile_pic']}' alt='{$UI['full_name']}' width='50' height='50' />\n";
            echo 
    "data: <p style='float:right;'>"Agotime($msg[sent]) ."</p>\n";
            echo 
    "data: <a style='position:absolute;' href='/profile?uid={$UI[id]}'>{$UI['full_name']}</a><br />\n";
            echo 
    "data: <h4>@{$msg['from']}</h4>\n";
            echo 
    "data: <p>{$msg['message']}</p>\n";
            echo 
    "data: </article>\n\n";
        }
        
    sleep(7);
    }
    flush();
    ob_flush(); 
    I checked the streaming page as well and it is blank. Everything was working in a sense before I updated the SQL from "OR" to "AND".
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps

  • #4
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    bump.
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •