I am trying to build a live chat enquiry form in a website using html server sent events.
I am following this tutorial as base for it
Here is what i intend to do based on this tutorial
At client side User is asked for username .This is used as a primarykey for unique chat log identification .
The client and server listens for server sent events by listening to following php pages.
When client press send button chat is sent to this php page which inserts it into database and outputs UserName
Client: var serverSource = new EventSource('ServerListener.php');
Server: var clientSource= new EventSource('ClientListener.php');
Here there are two php files one for inserting to database chat from client side and other for inserting for reply from server
Both these files also has another function when user sends to
chattoserver.php
it also notify server of new chat received along with user name it search for unread rows and fetch it and appennd to chat window similarly when server replies it is send tochatreplyreceived.php
where it writes database and notify the client .So in case of multiple clients listening to this file .Reading the message correct client with user name can search database for reply and appending to chat log .
Here the dates are showed correctly in both listner pages but the message is not shown .And text file contains messages sent .When checked packets using firefox network analysis tool.It contains flushed out message in response part.But the listners failing to detect it .What is that i am doing wrong that messages are not being echoed ?
ClientTest.php
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input[type=text]').bind("enterKey",function(e){
//alert("Enter");
});
$('input[type=text]').keyup(function(e){
if(e.keyCode == 13)
{
$(this).trigger("enterKey");
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementsByTagName("h1").innerHTML = xmlhttp.responseText;
//alert("hello");
}
}
$('div.chat-box-content').append('<div class="clear"></div><span class="right">'+$(this).val()+ '</span>');
xmlhttp.open("GET", "ClientListener.php?Message='"+$(this).val()+"'" , true);
xmlhttp.send();
// xmlhttp.open("POST","ClientListener.php",true);
// xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
// xmlhttp.send("Message='"+$(this).val()+"'");
}
});
}
);
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("ServerListner.php");
source.onmessage = function(event) {
//alert("Data Received");
//alert(event.data);
//alert(document.getElementsByClassName("chat-box-content").innerHTML);
// document.getElementsByClassName("chat-box-content").innerHTML +='<div class="clear"></div><span class="left">'+event.data+ '</span> <br>';
//if(event.data !==''){
$('div.chat-box-content').append('<div class="clear"></div><span class="left">'+event.data+ '</span>');
//}
};
} else {
document.getElementById("chat-box-content").innerHTML = "Sorry, your browser does not support server-sent events...";
}
</script>
<style type="text/css">
body {
height:3000px
}
.chat-box {
font:normal normal 11px/1.4 Tahoma, Verdana, Sans-Serif;
color:#333;
width:200px;
/* Chatbox width */
border:1px solid #344150;
border-bottom:none;
background-color:white;
position:fixed;
right:10px;
bottom:0;
z-index:9999;
-webkit-box-shadow:1px 1px 5px rgba(0, 0, 0, .2);
-moz-box-shadow:1px 1px 5px rgba(0, 0, 0, .2);
box-shadow:1px 1px 5px rgba(0, 0, 0, .2);
}
.chat-box > input[type="checkbox"] {
display:block;
margin:0 0;
padding:0 0;
position:absolute;
top:0;
right:0;
left:0;
width:100%;
height:26px;
z-index:4;
cursor:pointer;
opacity:0;
filter:alpha(opacity=0);
}
.chat-box > label {
display:block;
height:24px;
line-height:24px;
background-color:#344150;
color:white;
font-weight:bold;
padding:0 1em 1px;
}
.chat-box > label:before {
content:attr(data-collapsed)
}
.chat-box .chat-box-content {
padding:10px;
display:none;
}
/* hover state */
.chat-box > input[type="checkbox"]:hover + label {
background-color:#404D5A
}
/* checked state */
.chat-box > input[type="checkbox"]:checked + label {
background-color:#212A35
}
.chat-box > input[type="checkbox"]:checked + label:before {
content:attr(data-expanded)
}
.chat-box > input[type="checkbox"]:checked ~ .chat-box-content {
display:block
}
span
{
display: inline-block;
max-width: 200px;
background-color: white;
padding: 5px;
border-radius: 4px;
position: relative;
border-width: 1px;
border-style: solid;
border-color: grey;
}
left
{
float: left;
}
span.left:after
{
content: "";
display: inline-block;
position: absolute;
left: -8.5px;
top: 7px;
height: 0px;
width: 0px;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
border-right: 8px solid white;
}
span.left:before
{
content: "";
display: inline-block;
position: absolute;
left: -9px;
top: 7px;
height: 0px;
width: 0px;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
border-right: 8px solid black;
}
span.right:after
{
content: "";
display: inline-block;
position: absolute;
right: -8px;
top: 6px;
height: 0px;
width: 0px;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
border-left: 8px solid #dbedfe;
}
span.right:before
{
content: "";
display: inline-block;
position: absolute;
right: -9px;
top: 6px;
height: 0px;
width: 0px;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
border-left: 8px solid black;
}
span.right
{
float: right;
background-color: #dbedfe;
}
.clear
{
clear: both;
}
input[type="text"]{
width:96%;
margin:1%;
}/*INSERT INTO `users`( `userRole`, `userName`, `createdOn`, `emailId`, `is_active`, `password`) VALUES (1,'admin_chat',NOW(),'[email protected]',1,'123456') ON DUPLICATE KEY */
</style>
</head>
<body>
<div class="chat-box">
<input type="checkbox" />
<label data-expanded="Close Chatbox" data-collapsed="Open Chatbox"></label>
<div class="chat-box-content">
<span class="left">messmessage</span>
<div class="clear"></div>
<span class="right">messagemessagemessage</span>
<div class="clear"></div>
<span class="left">messagemessagsage</span>
<div class="clear"></div>
<span class="right">messagemessagemessage</span>
</div>
<input type="text" />
</div>
</body>
</html>
ClientListener.php
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
//header("refresh: 5;");
if (ISSET($_GET['Message'])) {
$msg =$_GET['Message'];
if(!empty($msg)){
$fp = fopen("_chat.txt", 'a');
fwrite($fp,$msg."\n\n");
fclose($fp);
}
echo "data: $msg\n\n";
flush();
}
?>
ServerTestPage.php
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input[type=text]').bind("enterKey",function(e){
//alert("Enter");
});
$('input[type=text]').keyup(function(e){
if(e.keyCode == 13)
{
$(this).trigger("enterKey");
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementsByTagName("h1").innerHTML = xmlhttp.responseText;
//alert("hello");
}
}
$('div.chat-box-content').append('<div class="clear"></div><span class="right">'+$(this).val()+ '</span>');
xmlhttp.open("GET", "ServerListner.php?Message='"+$(this).val()+"'" , true);
xmlhttp.send();
// xmlhttp.open("POST","ServerListner.php",true);
/// xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
// xmlhttp.send("Message='"+$(this).val()+"'");
}
});
}
);
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("ClientListener.php");
source.onmessage = function(event) {
//alert("Data Received");
alert(event.data);
//alert(document.getElementsByClassName("chat-box-content").innerHTML);
// document.getElementsByClassName("chat-box-content").innerHTML +='<div class="clear"></div><span class="left">'+event.data+ '</span> <br>';
// if(event.data!==''){
console.log(event.data);
$('div.chat-box-content').append('<div class="clear"></div><span class="left">'+event.data+ '</span>');
// }
};
} else {
document.getElementById("chat-box-content").innerHTML = "Sorry, your browser does not support server-sent events...";
}
</script>
<style type="text/css">
body {
height:3000px
}
.chat-box {
font:normal normal 11px/1.4 Tahoma, Verdana, Sans-Serif;
color:#333;
width:200px;
/* Chatbox width */
border:1px solid #344150;
border-bottom:none;
background-color:white;
position:fixed;
right:10px;
bottom:0;
z-index:9999;
-webkit-box-shadow:1px 1px 5px rgba(0, 0, 0, .2);
-moz-box-shadow:1px 1px 5px rgba(0, 0, 0, .2);
box-shadow:1px 1px 5px rgba(0, 0, 0, .2);
}
.chat-box > input[type="checkbox"] {
display:block;
margin:0 0;
padding:0 0;
position:absolute;
top:0;
right:0;
left:0;
width:100%;
height:26px;
z-index:4;
cursor:pointer;
opacity:0;
filter:alpha(opacity=0);
}
.chat-box > label {
display:block;
height:24px;
line-height:24px;
background-color:#344150;
color:white;
font-weight:bold;
padding:0 1em 1px;
}
.chat-box > label:before {
content:attr(data-collapsed)
}
.chat-box .chat-box-content {
padding:10px;
display:none;
}
/* hover state */
.chat-box > input[type="checkbox"]:hover + label {
background-color:#404D5A
}
/* checked state */
.chat-box > input[type="checkbox"]:checked + label {
background-color:#212A35
}
.chat-box > input[type="checkbox"]:checked + label:before {
content:attr(data-expanded)
}
.chat-box > input[type="checkbox"]:checked ~ .chat-box-content {
display:block
}
span
{
display: inline-block;
max-width: 200px;
background-color: white;
padding: 5px;
border-radius: 4px;
position: relative;
border-width: 1px;
border-style: solid;
border-color: grey;
}
left
{
float: left;
}
span.left:after
{
content: "";
display: inline-block;
position: absolute;
left: -8.5px;
top: 7px;
height: 0px;
width: 0px;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
border-right: 8px solid white;
}
span.left:before
{
content: "";
display: inline-block;
position: absolute;
left: -9px;
top: 7px;
height: 0px;
width: 0px;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
border-right: 8px solid black;
}
span.right:after
{
content: "";
display: inline-block;
position: absolute;
right: -8px;
top: 6px;
height: 0px;
width: 0px;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
border-left: 8px solid #dbedfe;
}
span.right:before
{
content: "";
display: inline-block;
position: absolute;
right: -9px;
top: 6px;
height: 0px;
width: 0px;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
border-left: 8px solid black;
}
span.right
{
float: right;
background-color: #dbedfe;
}
.clear
{
clear: both;
}
input[type="text"]{
width:96%;
margin:1%;
}/*INSERT INTO `users`( `userRole`, `userName`, `createdOn`, `emailId`, `is_active`, `password`) VALUES (1,'admin_chat',NOW(),'[email protected]',1,'123456') ON DUPLICATE KEY */
</style>
</head>
<body>
<div class="chat-box">
<input type="checkbox" />
<label data-expanded="Close Chatbox" data-collapsed="Open Chatbox"></label>
<div class="chat-box-content">
<span class="left">messmessage</span>
<div class="clear"></div>
<span class="right">messagemessagemessage</span>
<div class="clear"></div>
<span class="left">messagemessagsage</span>
<div class="clear"></div>
<span class="right">messagemessagemessage</span>
</div>
<input type="text" />
</div>
</body>
</html>
ServerListener
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
//header("refresh: 3;");
if (ISSET($_GET['Message'])) {
$msg =$_GET['Message'];
if(!empty($msg)){
$fp = fopen("_chat.txt", 'a');
fwrite($fp,$msg."\n\n");
fclose($fp);
}
echo "data: $msg\n\n";
flush();
}
?>
console.log
if there is any to show the problem you're having? – Egestion