2

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 -->
4
  • Why not checking out something like AJAX or NodeJS? Here are a couple of tutorials I found: 1. code.tutsplus.com/tutorials/… 2. uditalias.github.io/chat-nodejs Commented Mar 16, 2014 at 20:50
  • @user3426757 this is not the ideal way to deal with this sort of app. you need to read about: socket.io and webrtc.org Commented Mar 16, 2014 at 20:51
  • @user3426757 your approach may lead to an IP block of any of the users chatting due to multiple requests. However, there's a hack and a much less elegant way to achieve what you're trying to achieve using AJAX and PHP by attempting to refresh the page after a set interval. Commented Mar 16, 2014 at 20:57
  • I can't install NodeJS on the server, so that's out. A set refresh interval would work. Commented Mar 16, 2014 at 21:17

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the info! I know extremely minimal JS, so how would I incorporate a set interval check?
Again, not recommended :-), but something like this: setInterval(function() { $.get('/updates.php').done(function (data) { $("#chat").append(data) }); }, 5000); assuming you include jQuery and use some object with the ID 'chat' to show the chat messages. The PHP script should sort out if there where updates in the last 5 seconds, or you should send along a last_id to the updates.php....
Thanks! I did setInterval(function() { $.get('../libs/Chat/chatContent.file').done(function (data) { $("#Chat").append(data) }); }, 3000); }); - is there a way to change the .append to make it just refresh the entire thing without re-adding all the messages. I'm not entirely sure how to set up an update system, so that's why I'm asking about this way.
That would be $('#Chat').html(data);
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.