yiichat /

Filename Size Date modified Message
18 B
368 B
3.0 KB
5.0 KB
424 B
9.8 KB
4.7 KB
369 B
84.1 KB


The most simple jQuery based chat in the world for Yii based applications.

Not dependent on any data model, it is an abstract chat component, it will connects to a class who implements a required interface. You can provide your own class or mechanism to provide real data to this chat.

autor: Christian Salazar.


Screenshot for YiiChat


1) Install the extension in the directory:

    using git:
        cd /home/yourapp/protected/extensions
        git clone
    or, simply uncompress your zipfile into your protected extensions directory

2) In config/main, in your imports, add:


3) Edit your file: 'protected/controllers/siteController.php' and add the following the array at actions():

class SiteController extends Controller
    public function actions()
        return array(
            'yiichat'=>array('class'=>'YiiChatAction'), // <- ADD THIS LINE

4) In any view in were you decide to insert this chat (views/site/index.php, as an example), please configure the widget in this way:

<h1>Yii Chat Demo</h1>
<div id='chat'></div>
        'chat_id'=>'123',                   // a chat identificator
        'identity'=>1,                      // the user, Yii::app()->user->id ?
        'selector'=>'#chat',                // were it will be inserted
        'minPostLen'=>2,                    // min and
        'maxPostLen'=>10,                   // max string size for post
        'model'=>new MyYiiChatHandler(),    // the class handler.
                                            // MyYiiChatHandler is a demo
                                            // you must create your own class 
                                            // and implements IYiiChat interfac
                                            // Read about 'Database Support'

        'data'=>'any data',                 // data passed to the handler

        // success and error handlers, both optionals.
        'onSuccess'=>new CJavaScriptExpression(
            "function(code, text, post_id){   }"),
        'onError'=>new CJavaScriptExpression(
            "function(errorcode, info){  }"),

How it works.

The chat depends on a interface (IYiiChat), any class who implements this interface can serve a YiiChat Application. For demo pruposes i provide a class named 'MyYiiChatHandler', this chat handler provides a functionality based on random messages, you are responsible for connect this class to a real database.

All actions (internals in YiiChat) are invoked via ajax requests automatically by the automatic action provided in site/controller (see step 3).


For now, you must edit the provided yiichat.css file, located in assets/, in a future version this file must be specified in the widget arguments.

How to specify my own class handler ?

Edit the attribute 'model' in the widget setup. As an example, suppose you have your own class that implements IYiiChat:


now, you pass this class as an instance to this widget in this way:

    'model'=>new MyOwnHandler(),  
    // will works because your components dir is visible in your 
    // imports at config/main

Database Support

To get this widget working using your database:

1) In your database, import the script 'post.sql'. 2) In your components, create a class named: 'components/ChatHandler.php', as follows:

class ChatHandler extends YiiChatDbHandlerBase {
    // in any time here you can use this available methods:
    //  getData(), getIdentity(), getChatId()
    protected function getDb(){
        // the application database
        return Yii::app()->db;
    protected function createPostUniqueId(){
        // generates a unique id. 40 char.
        return hash('sha1',$this->getChatId().time().rand(1000,9999));      
    protected function getIdentityName(){
        // find the identity name here
        // example: 
        //  $model = MyPeople::model()->findByPk($this->getIdentity());
        //  return $model->userFullName();
        return "jhonn doe"; 
    protected function getDateFormatted($value){
        // format the date numeric $value
        return Yii::app()->format->formatDateTime($value);
    protected function acceptMessage($message){
        // return the message (you can alterate it). 
        // return an empty string to reject it.
        return CHtml::encode($message); // encode to avoid html

3) In your widget, instead of using the provided MyYiiChatHandler, use your new ChatHandler:

        'model'=>new ChatHandler(), // the class handler using database


Due to business requirements i provide this chat as-is, it can be modeled to fit any business rules, because it does not depends on any database or similar structure. The demo class provides the most simple case to implements a functional chat widget. It is your responsability to connect it to a real database.

enjoy it. hope it will be usefull for you. Issues and comments are welcome.