4

I coded a website using HTML5 Server Sent Events and it's working like charm on Godaddy shared hosting, however the same site with exactly the same code isn't working on 101domain shared hosting. Rest all is working fine, except chat functionality using Server Sent Events.

How to find the errors in the script.

Here is the HTML page, with SSE javascript code.

<?php
    $get_value_this_url = "globalchat";
?>
<div id="chat">
            <div id="chats-div">
            <p id="tip">(tip! kickstart a discussion by sharing this page)</p>
            <ul id="chats-ul">
            <?php
            /*This session id will be used to fetch the latest chat via SSE*/
            $_SESSION["id"] = 1;
            if($stmt = $con->prepare("SELECT `icicinbbcts_id`, `icicinbbcts_user`, `icicinbbcts_chats` FROM `icicinbbcts_chats` WHERE `icicinbbcts_video_id` = ? ORDER BY `icicinbbcts_id` DESC LIMIT 25")){
                $stmt->bind_param("s", $video_id);
                $video_id = $get_value_this_url;
                if ($stmt->execute()) {
                $stmt->bind_result($id, $user, $chats);
                $stmt->store_result();
                if($stmt->num_rows() > 0){
                    $_SESSION["offset"] = $stmt->num_rows;
                }
                while ($stmt->fetch()) {
                        //print_r($user .": ". $chats."<br>");
                        echo '<li class="chats"><span id="nicknameChats">'.$user.'</span>: '.$chats.'</li>';
                        //echo '<span class="line-spacing"></span>';
                        $_SESSION["offset"] = $stmt->num_rows;
                        if($_SESSION["id"] < $id){
                            $_SESSION["id"] = $id;
                        }
                    }
                }else
                echo $stmt->error;
            }else
            echo $stmt->error;
            $stmt->free_result();
            $stmt->close();
            ?>
            <script>
            if(typeof(EventSource) !== "undefined") {
                var source = new EventSource("auto_update.php?r=<?php echo $get_value_this_url; ?>");
                source.onmessage = function(event) {
                var obj = JSON.parse(event.data);
                        $("#chats-ul").append('<li class="chats"><span id="nicknameChats">'+obj.user+'</span>: '+obj.chats+'</li>');
                        $('#chats-ul').scrollTop($(window).height());
                };
            } else {
                document.getElementById("chats-ul").innerHTML = "Your browser doesn't support a part of HTML5, so please use modern browsers like Chrome, Firefox, etc.";
            }
            </script>
            </ul>
            </div>
            <input id="text-area"  class="q" name="text-input" form="textForm" maxlength="140" placeholder="Type to comment" ></input>
            </div>

And here's the server side PHP script.

<?php session_start(); ?>
<?php //error_reporting(E_ALL); ?>
<?php require "connection.php";?>
<?php
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');

if($stmt = $con->prepare("SELECT `icicinbbcts_id`, `icicinbbcts_user`,     `icicinbbcts_chats` FROM `icicinbbcts_chats` WHERE `icicinbbcts_video_id` = ?   AND `icicinbbcts_id` > ? ORDER BY `icicinbbcts_id` DESC")){
    $stmt->bind_param("si", $video_id, $row_id);
    $row_id = $_SESSION["id"];
    $video_id = mysqli_real_escape_string($con, strip_tags($_GET["r"]));
    if ($stmt->execute()) {
        $stmt->bind_result($id, $user, $chats);
        $stmt->store_result();
            if($stmt->num_rows > 0){
                while ($row = $stmt->fetch()) {
                    if($_SESSION["id"] < $id){
                            $send = array("user" => $user, "chats" => $chats);
                            echo "retry: 100\n";
                            echo "data: ".json_encode($send)."\n\n";
                            ob_end_flush();
                            flush();
                    $_SESSION["id"] = $id;
                    }
                }
            }

        }else
        echo $stmt->error;
    }else
    echo $stmt->error;
$stmt->close();
?>

The same above code works perfectly on Godaddy shared hosting, however the save code just isn't working on 101domain.

I tried putting charset UTF-8 in both PHP and HTML, but that din't help, I tried adding ob_flush in server side PHP script and it din't help too.

There's nothing wrong with connection.php because same file on other pages works fine. On chat functionality the chat messages are getting inserted into the database, however it's not showing on the page, unless we refresh the page.

How to check for errors and how to debug Server Sent Events?

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

I did console.log and didn't find the log in chrome, when I placed console.log code at the point shown below in the code.

<script>
            if(typeof(EventSource) !== "undefined") {
              console.log("SSE is supported"); //placing console.log here, displays the message in chrome developer tool
                var source = new EventSource("auto_update.php?r=<?php echo $get_value_this_url; ?>");
                source.onmessage = function(event) {
                    console.log("message isn't received from server"); //placing console.log here doesn't display any log message in Chrome Developer Tool
                var obj = JSON.parse(event.data);
                        $("#chats-ul").append('<li class="chats"><span id="nicknameChats">'+obj.user+'</span>: '+obj.chats+'</li>');
                        $('#chats-ul').scrollTop($(window).height());
                };
            } else {
                document.getElementById("chats-ul").innerHTML = "Your browser doesn't support a part of HTML5, so please use modern browsers like Chrome, Firefox, etc.";
            }
            </script>

So from the console.log above, we see that the SSE script is not receiving messages from backend, and backend code is shown above, can you show what's wrong with the backend code?

Yogie
  • 986
  • 2
  • 12
  • 14
  • have you tried console.log messages in your html page to see what the browser gets? – chriskelly Sep 18 '15 at 23:42
  • @AdamBuchananSmith I am 100% sure, that you didn't take the difficulty of reading the entire question and you judged the question based on its title, and the worst thing, you down voted it, just based on your assumption. Now this question will get less attention just because of your laziness. My heart has a lot to say about you, however I'll listen to my mind for now. The question is not "how to get PHP errors?" but "how to get the errors about SSE, i.e console.log. I'll read about console.log and will try it Whether you help or create obstacle, I'll find the answer and will make it working. – Yogie Sep 19 '15 at 00:04
  • @chriskelly I did console.log and place it at a line shown in the code (just updated my question) and this doesn't display any log message in Chrome Developer Tool, so I analyse it's not fetching messages from the server, do you any idea, what may be wrong? – Yogie Sep 19 '15 at 19:10
  • Could it be a database issue? Are you sure num_records is greater than 0? Is ob_end_flush() actually getting called? Can you send back the number of messages and console.log() that first – chriskelly Sep 19 '15 at 19:56
  • @chriskelly num_records is greater than 0, I tested with an echo statement from within the while loop, also I did some hit and trial method, and found that `if($_SESSION["id"] < $id)` is not evaluating to true, and if I make it true as `if(true)` SSE works fine, so something seems to be wrong with SESSION thing, however exactly the same code runs on Godaddy shared hosting with SSL disabled, but not on 101domain shared hosting with SSL enabled. So one of the differences in servers is the SSL, could it lead to problems with SESSION? – Yogie Sep 19 '15 at 20:54
  • Where are you getting $id from? Is it a $_REQUEST variable? Could it be that the php globals are enabled by godaddy? Have you tried comparing the output of the phpinfo() function on your godaddy and 101domain to see if they are configured differently? If it works on one site and not the other there must be some site specific difference. – chriskelly Sep 19 '15 at 21:13
  • @chriskelly There's different version of PHP on both the servers. Godaddy has PHP 5.4.43 and 101domain has PHP 5.3.3, so I am now finding out, the differences so I can turn off/on the different things. – Yogie Sep 19 '15 at 21:27
  • 1
    Great to hear you have narrowed it down. Good luck with the last steps. This proves its not a duplicate question too! – chriskelly Sep 20 '15 at 09:42
  • 1
    @Yogie I never down voted your question :/ – Adam Buchanan Smith Sep 21 '15 at 15:50
  • @chriskelly The issue was the difference in PHP versions, and once I got the PHP Version upgraded by talking to support, issue fixed. Please feel free to post this as an answer, and I'll accept it as the correct answer. Will also upvote you. Thanks for help, console.log helped me to reach where I found it's different versions. – Yogie Sep 22 '15 at 22:57
  • @chriskelly I have talked to support and they have reopened this question, now you can answer this question. Thanks! – Yogie Sep 24 '15 at 03:53
  • i would like to ask how to see errors on the server, when use SSE. I made an error in the server on purpose then I checked the network tab [answer/request] but it shows nothing. but before the error was there. extrange – vincent thorpe Aug 27 '22 at 12:58

2 Answers2

1

Since the result it works with one provider but not another, it is probably a server configuration issue.

Here are some things you can try to narrow down the issue:

  • In the javascript script use console.log(event) to check if you are getting anything from the server
  • Check the network tab in the developer tools area of browsers (in Chrome choose other to view server sent events).

If there is no output then try narrowing the problem down on the server. Resolev SSE output early to see if SSE mechanism works. Bit by bit resolve the SSE later and later until it breaks. If there is a difference on a specific line between providers it may be due to the PHP version. To compare php versions run <?php phpinfo() ? in a php file.

If you find a difference contact your provider!

chriskelly
  • 7,526
  • 3
  • 32
  • 50
  • 1
    Sorry, but this does not answer the question "How to see errors about server sent events?" (in Javascript). It's a guide, not an answer. It's more expected to treat `sse_source.onerror = function (evt) {}` and get error information from there, however, it seems not possible. – Avatar Mar 24 '22 at 06:56
  • @Avatar This is an old one and it's been a long time since I worked with SSE's. Go ahead and provide a better answer. I'll upvote it. – chriskelly May 02 '22 at 22:46
  • 1
    Basically you have to catch any PHP error and send/echo it back by SSE. – Avatar May 03 '22 at 08:39
0

You want to report errors?

<?php
// Turn off error reporting
error_reporting(0);

// Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Report all errors
error_reporting(E_ALL);

// Same as error_reporting(E_ALL);
ini_set("error_reporting", E_ALL);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
?>
Adam Buchanan Smith
  • 9,422
  • 5
  • 19
  • 39
  • This is not what I was looking for, but to see the errors of javascript in html, when it's not working fine. I won't down vote though. – Yogie Sep 19 '15 at 00:09