Saturday 15 March 2014

Filled Under:

how to create Redis, chat application in php

                 
                  many of my blog  readers are want to get better chatting system in php.and i have been working with many powerfull technologies like php with node.js etc..a Redis is a open source advanced keyvalue database storage system and it is same as working like NoSQL database. redis operations can be executed on the server side. it is mainly used for cache to create a speed up web application. you can download full source code absolutley free.



here you want first add below extension on php.ini
extension=redis.so

windows
first download redis for click here go to
bin directory then extract redisbin.zip and run the redis-server.exe file.

now add below extension on php.ini

extension php_redis-2.1.3-5.3-ts.dll

Redis Sample Code

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$key="Key_Name"
$redis->set($key, 'Key Value');
echo $redis->get($key);
?>

index.php



<script src="js/jquery.1.9.1.min.js"></script>
<script src="js/jquery.timeago.js"></script>
<script src="js/jquery.livequery.js"></script>
<script src="js/jquery-linkify.min.js" ></script>
<script type="text/javascript">
//HTML encoding. 
function htmlEscape(str) {
return String(str).replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
//Jquery ajax call. 
function ajax_data(typeMethod,url,dataString,success){
$.ajax({
type:typeMethod,
url:url,
data :dataString,
dataType:"json",
cache:false,
timeout:20000,
beforeSend :function(data) { },
success:function(data){
success.call(this, data);
},
error:function(data){
alert("Error In Connecting");
}
});
}
// Appending values in ol#update tag
function Action(json){
$("ol#update").empty();
var b="";
for (var i = 0, len = json.length; i < len; i++)
var msg=htmlEscape(json[i][2]);
var time=htmlEscape(json[i][1]);
var name=htmlEscape(json[i][0]);         
b='<li><b>'+name+': </b>'+msg+' - <a href="#" class="timeago" title="'+time+'"></a></li>'; 
$("ol#update").prepend(b); 
$(".timeago").timeago();  
}
// Inserting records into chat table
$(document).ready(function()
{
// Text to link 
$("#update li").livequery(function()
{
$(this).linkify({
target: "_blank"
});
});
var user='<?php echo $user_sessions;?>';
// Requesting to chat_get.php every 2 seconds
if(user.length>0)
{
var auto_refresh = setInterval(function ()
{
var dataString = 'user='+ user;
ajax_data("POST","chat_get.php",dataString, function(data) {
Action(data);
});
}, 2000);
}
$('#post').click(function()
{
var content = $("#content").val();
var dataString = 'user='+ user + '&content=' + content;
if(boxval.length > 0)
{
ajax_data("POST","chat.php",dataString, function(data) {
Action(data);
});
$('#content').val('').focus();
}
return false;
});
});
</script>
// HTML Code
<ol id="update" > </ol>
<input type="text" name="content" id="content" autocomplete="off" maxlength="250"/>
<input type="submit" value="Post" id="post" />

redis_config.php

<?php
$key="Key_Name"; // Redis Key Name
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
?>

chat.php

<?php
include 'redis_config.php';
include 'arrayToJSON.php';
if(isset($_POST['content']) && isset($_POST['user']) )
{
if($redis && !empty($_POST['content']) &&  !empty($_POST['user']))
{
$message=$_POST['content']; //Message
$user=$_POST['user']; //Username 
$time=date("c", time()); //Timestamp 
$redis->lpush($key, serialize(array($user,$time, $message)));
}
$data=$redis->LRANGE($key,0,30);
$newData = arrayToJSON($data);
echo $newData;
}
?>

chat_get.php

<?php
include 'redis_config.php';
include 'arrayToJSON.php';
if(isset($_POST['user']))
{
$data=$redis->LRANGE($key,0,30);
$newData = arrayToJSON($data);
echo $newData;
}
?>

it will be return json output like below,

[
["User One","2013-12-30T09:18:37+01:00","Hi"],
["User Two","2013-12-30T08:57:00+01:00","Hello"],
["User One","2013-12-28T18:46:41+01:00","Hi Everyone"],
......
......
]

arrayToJson.php

<?php
function arrayToJSON($arr)
if(count($arr) > 0)
{  
for($i=0; $i<count($arr); $i++)
{
$data = @unserialize($arr[$i]);
if($data !== false )
{
$arr[$i] = unserialize($arr[$i]); 
}
}
return json_encode($arr);
}
?>






1 comments: