Reputation: 25
We have an instant-messaging chat system of sort on our site. It's just basically an embedded box that lists the text with a bar at the bottom to submit new text that gets written to a file.
Currently when you submit a new message you have to refresh the page in order for it and others to show up. How do I make it so when there's a new message it automatically refreshes or displays the message?
<!-- BEGIN CHAT -->
<div id='categories' class='ipsLayout_content clearfix'>
<div id='' class='brown_box_forum brown_box_stack'>
<table class="forum_group">
<tbody>
<tr>
<td style="background: -webkit-gradient(linear, left top, left bottom, from(#412E0E), to(#513A13));
background: -moz-linear-gradient(top, #412E0E, #513A13);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#412E0E', endColorstr='#513A13');
-ms-filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#412E0E', endColorstr='#513A13');padding-top: 1px;
font-size: 14px; font-weight:bold;" align="center">
<div id="toggleBoardStats" class="groupname" style="text-align:center;cursor:pointer;">RuneMechanics Chat</div>
</td>
</tr>
</tbody>
</table>
<table class="forum_group" id="">
<tbody>
<tr class="spacer"><td colspan="5"></td></tr>
<tr>
<td>
<div style="height: 165px; width: 100%;">
<div id="" style="height:11px; overflow-y: auto; word-wrap: break-word; text-align: left; padding: 0px; font-style:none; font-size:12px; padding-bottom:7px; padding-left: 6px;">[00:00:00] GLOBAL: <i>There is a slight delay when sending messages, please be patient. | Chatbox Feedback: <a href="**/forums/?showtopic=372">Here</a></i></div>
<div id="ChatStream" style="height: 110px; overflow-y: auto; word-wrap: break-word; text-align: left; padding: 5px; scrollbar-face-color: #3B3B3B;"></div>
<div style="bottom: 5px; width: 100%; margin-top: 5px; padding-bottom: 5px;">
<form id="Messenger" method="POST">
<span style="padding: 5px; padding-left: 3px;">
<input id="messageText" type="text" style="width: 99%; background:#4D3F27; border: none; color: #D6C6AB;" name="message" placeholder="Message..." autocomplete="off" />
</span>
<button id="sendMessage" style="height:0; width:0; background: #392c14; border:none;">Send</button>
</form>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript">
var chatFrozen = false;
var chatOpen = true;
jQuery(function($) {
$('#ChatStream').load('../libs/Chat/chatContent.file', function(){
$('#ChatStream').animate({ scrollTop: $('#ChatStream')[0].scrollHeight }, 500);
});
window.setInterval(function(){
if ( !chatFrozen && chatOpen )
{
if ( $('#ChatStream').scrollTop() >= ( $('#ChatStream')[0].scrollHeight ) )
{
$('#ChatStream').animate({ scrollTop: $('#ChatStream')[0].scrollHeight }, 500);
}
}
}, 200);
$('#sendMessage').click(function(e) {
e.preventDefault();
$.post('../libs/Chat/Message.php', $('#Messenger').serialize(), function( data ) {
$('#ChatStream').animate({ scrollTop: $('#ChatStream')[0].scrollHeight }, 500);
});
$('#messageText').val('');
if ( $('#ChatStream').scrollTop() >= ( $('#ChatStream')[0].scrollHeight - 200 ) )
{
$('#ChatStream').animate({ scrollTop: $('#ChatStream')[0].scrollHeight}, 500);
}
});
});
</script>
<!-- END CHAT -->
Upvotes: 2
Views: 84
Reputation: 130
You can also consider using Websocket in PHP such as http://socketo.me/ to create real time, bi-directional applications between clients and servers. I believe Pusher uses the same techology.
Upvotes: 0
Reputation: 4532
You want to push information to a client instead of the client requesting it. If the client is going to request the information, he should do a request every X seconds just to check if there is some new message. That is not very scalable and gives a delay of at least a couple of seconds.
The problem is, PHP is a language that is very much intended to be used for serving data when a request is made. If you have no other languages available I would strongly advise to use an external service like Pusher.com or Pubnub.com to create real-time applications.
These services provide you with a client-side Javascript library and a server-side PHP library. You send a push message in your PHP script and it is instantly received by the clients subscribed to a channel. Great for chat applications like yours.
Upvotes: 2