Reputation: 7419
This has been implemented in many websites lets say odesk or so.What I am thinking is Implement a message sending scheme which notifies the user that they have received message.
for example
You send message to me and I would be shown message Red icon from green. Obviously we would need a database table to store sender id receiver Id and so on but how do we implement it in such a way that user does not need to hit refresh button.
I am new to web from c# background so do not know as many ways.
I am developing it in Yii. Few suggestions would be great
Upvotes: 2
Views: 925
Reputation: 17478
You can do this by using a simple periodic refresh javascript method.
Something like this in the view that has the message indicator:
<?php Yii::app()->clientScript->registerScript('autoupdate-div-inbox-update',
"setInterval(function(){
// do an ajax call to server to check for new messages
// using jquery's ajax method
$.ajax({
url: 'http://example.com/index.php?r=controller/action',// this is the url that will check for new messages for the current user
success: function(data) {// data is the data returned from the server
if(data){
// update your new message div
// you can show your red icon here
}
}
});
return false;
},1000);"
);
?>
So what is happening is that the setInterval method executes the function every 1000 milliseconds and the function checks for new messages using ajax.
If you don't know ajax in yii, then check the following for the controller action:
public function actionMessages(){
// check for new messages in the db
$xyz = checkMessage();
// assuming checkMessage returns the number of new messages if any or false if none
// whatever we echo will be available to the javascript we wrote in the data variable
echo $xyz;
}
Read more about the timing methods in javascript.
This pattern is also called polling, there are also other popular methods like long polling, and server push, which i'm not very familiar with, but you should check them out too before deciding on a pattern.
Hope this helps, ask for clarifications, if any.
Upvotes: 3