Simple socket chat

0.2.9 2017-10-10 19:08 UTC



1) Download composer:

php -r "copy('', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

2) Install:

php composer.phar require programmis/socket-chat

Starting chat server

$loader = require __DIR__ . '/vendor/autoload.php';

$server = new chat\Server();

or in your project

class Server extends \chat\Server
    /** @inheritdoc */
    public static function getConfigClass()
        return "Your config class implemented 
            from \chat\interfaces\ConfigInterface
            or extend from \chat\libs\Config";
        //return MyConfig::class;

and in your config class

class MyConfig extends \chat\libs\Config
    //you can override any chat class

and to start

$server = new your\project\namespace\Server();

//or for you daemon
$server->tick(); //in your daemon loop method

simple daemon provider and how to connect this chat to YII2 framework

popular methods

UserProcessor:createUser    //maybe init user from your database by $connection_info data
User:findOne                //find and return user in your database by user id
User:getAccessList          //return user ids list of access to send messages
User:getSendRight           //return user right for send messages
User:getInfo                //return array with user info
User:onConnect              //called if user connect
User:onDisconnect           //called if user disconnect
User:onChangeRecipient      //called if user change recipient
Message:getHistory          //find and return users messages in your database
Message:beforeSend          //called before send message
Message:afterSend           //called after send message
Chat:sendMessageText        //you may send any text message to user

Client side

<script src="js/socketChat.js"></script>

Init java constants

<script type="application/javascript">
    $(function () {
        <?= Server::fillJavaConstants(); ?>


socketChat.connection_type = "Maybe 'ws' or 'wss'"
socketChat.current_user_id = "Current user id in chat";
socketChat.socket_url = "You're server address : and port";
socketChat.send_on_enter = "If this true, then all messages sending by press on enter key, ctrl+enter default"
socketChat.recipient_id = 'Message recipient id, you can fill it before send messages'; = "Chat room name is required fill";
socketChat.hash = "You're secret hash for processing with UserProcessor";
socketChat.user_typing_timeout = "For auto disable user typing status";
socketChat.message_history_period = "For default request message history";

Functions;                                  //open connect with server
socketChat.close();                                 //close connect with server
socketChat.setMessageAreaId(id);                    //set textarea id for messages
socketChat.send();                                  //send message from message_area to socketChat.recipient_id 
socketChat.getUserList();                           //get all users in current room
socketChat.getUserInfo(user_id);                    //get info about user
socketChat.getMessageHistory(with_user_id, period); //get all messages for current user and with_user_id by period


socketChat.onConnect            //called if chat connected to server
socketChat.onDisconnect         //call if chat disconnected with server
socketChat.onMessageRender      //called if render message with "message" in parameter 
socketChat.onMessageListRender  //called if render message list with "message_list" in parameter
socketChat.onUserConnect        //called if user connect to chat with "user" in parameter
socketChat.onUserDisconnect     //called if user disconnect from chat with "user" in parameter
socketChat.onUserRemoved        //called if user removed from chat with "user" in parameter
socketChat.onAboutMeInfo        //about you info 
socketChat.onUserInfo           //called if received user info with "user" in parameter
socketChat.onUserList           //called if user list received with "user_list" in parameter
socketChat.onUserTypingStart    //called if user start typing with "user_id" in parameter
socketChat.onUserTypingEnd      //called if user end typing with "user_id" in parameter

For nginx ssl encrypt

Server::$port = 1337;
Server::$proxy_port = 1338;
Server::$proxy_connection_type = 'wss';

server {
    server_name localhost _;
    listen 1338 ssl;
    proxy_connect_timeout       600;
    proxy_send_timeout          600;
    proxy_read_timeout          600;
    send_timeout                600;

    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;

    root /home/;
    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

How to enable another config param see example how to connect this chat to YII2 framework

For example see index.php and socketChatDemo.js files

Sorry for my english