i have a messages chat on my video site .
it is not a comments , it is a chat runs in real time between users .
it is working like this :
- a setTimeout function runs every 3 seconds all the time .
- user1 send message .
- setTimeout function is working so user2 get the message Immediately (3s).
- setTimeout function working .
- users send / not send the setTimeout function still working .
- if stop this setTimeout function user2 will not see the message until refresh the page .
my hosting server company say this cause a heavy load on the cpu .
long polling , websocket , nodejs and other like these methods are not supported on my server .
so the only way i have to do , to convert this kind of code to Ajax .
but i dont have any knowledge of php/js coding .
here all my code , php , html and js :
and if you need more codes i will post them
$_POST['last_id'], 'first_id' => $_POST['first_id'], 'return_method' => 'html'));
if (!empty($messages_html)) {
$html = PT_LoadPage("messages/{$pt->config->server}/messages", array('MESSAGES' => $messages_html));
} else {
$html = PT_LoadPage("messages/ajax/no-messages");
}
$users_html = PT_GetMessagesUserList(array('return_method' => 'html'));
if (!empty($messages_html) || !empty($users_html)) {
$data = array('status' => 200, 'message' => $messages_html, 'users' => $users_html);
}
}
?>
‘);
}
if ($(‘#new-message’).val().length >= 1) {
$id = makeid();
formData.push({ name: ‘message_id’, value: $id });
$(‘.messages’).append(‘
‘);
$(‘#new-message’).val(”);
$(‘#new-message’).height(40);
$(‘.user-messages’).scrollTop($(‘.user-messages’)[0].scrollHeight);
} else {
$(‘#new-message’).val(”);
}
},
success: function(data,rew,rr) {
if (data.status == 400) {
$(‘.data_message[data-id=”‘ + $id + ‘”]’).remove();
swal({
title: ‘{{LANG error}}’,
text: data.message,
type: ‘error’,
confirmButtonColor: ‘#3085d6’,
cancelButtonColor: ‘#d33’,
confirmButtonText: ‘OK’,
buttonsStyling: true,
confirmButtonClass: ‘btn btn-success’,
});
}
else{
$(‘.data_message[data-id=”‘ + data.message_id + ‘”]’).html(data.message);
}
}
});
});