Web Instant Chat with Php+swoole: Phpwebim

Source: Internet
Author: User
Tags composer install server port

Web Instant Chat tool with Php+swoole implementation

  • Fully asynchronous non-blocking server that can support millions of TCP connections online at the same time

  • WEBSOCKET+COMET2 compatible protocols are supported for all kinds of browsers including IE

  • Has a complete UI interface

  • Support for features such as single chat/group chat/Group Chat

  • Support Send Emoticons

  • Support for permanent chat history saving

  • Instant content updates based on server push, Login/logout/status change/messages, etc.

    The latest version can be natively supported IE series browser, based on the HTTP long connection

    Installation

    Swoole extension

    PECL Install Swoole

    Swoole Frame

    Composer Install
    Run

    Configure the client directory to the Nginx/apache virtual host directory to make the client/index.html accessible. Modify the Client/config.js, IP and port for the corresponding configuration.

    PHP webim_server.php
    Detailed Deployment Instructions

    1. Install composer (PHP dependency package tool)

    | PHP mv Composer.phar/usr/local/bin/composer

    Note: If the PHP interpreter program is not set to the environment variable path, it needs to be set. Because composer file first behavior #!/usr/bin/env PHP, and cannot be modified. More detailed description of the composer: http://blog.csdn.net/zzulp/article/details/18981029

    2.composer Install

    Switch to Phpwebim project directory, execute instruction composer install, if slow

    Composer Install--prefer-dist

    3.ningx/apache configuration (This does not use the Web AppServer provided by Swoole_framework)

    Nginx

    server {     listen       ;     server_name  im.swoole.com;     Index index.shtml index.html index.htm index.php;     Root  /path/to/phpwebim/client;     Location ~. *\. (PHP|PHP5) $     {         fastcgi_pass  127.0.0.1:9000;         Fastcgi_index index.php;         Include fastcgi.conf;     }     Access_log  /library/webserver/nginx/logs/im.swoole.com  access;}

    Apache

    <virtualhost *:80>     documentroot "path/to/phpwebim/client"     ServerName im.swoole.com     AddType application/x-httpd-php. php     <directory/>         Options Indexes followsymlinks         allowoverride None         Require all granted         directoryindex index.php     </Directory> </VirtualHost>

    4. Modify the configuration phpwebim/config.php

    $config [' server '] = Array (     //Listen for host ' host ' = '     0.0.0.0 ',     //Listen for Ports     ' port ' = ' 9503 ',     // WebSocket URL address for use by the browser     ' url ' = ' ws://127.0.0.1:9503 ',);

      • Server.host Server.port is the IP and port of the Webim server, which is the WebSocket server, and the other selections are modified according to the specific circumstances
      • The server.url corresponds to the server IP or domain name and the port of the WebSocket service, which is the WebSocket address provided to the browser.
      • Webim.data_dir to modify the directory where the chat record is stored, you must have writable permissions

    5. Start the WebSocket server

    PHP phpwebim/webim_server.php

    IE does not support websocket and requires flashwebsocket emulation, modify the corresponding port in flash_policy.php, and then start flash_policy.php.

    PHP phpwebim/flash_policy.php

    6. Bind the host and access the Chat window (optional)

    If the URL uses ip:port directly, there is no need to set it.

    Vi/etc/hosts

    Increase

    127.0.0.1 im.swoole.com

    Open in Browser: http://im.swoole.com

    Quick understanding of Project architecture

    1. Directory structure

    + Phpwebim |-webim_server.php//websocket Protocol Server|-config.php//swoole Run Configuration|+ Swoole.ini//WebSocket protocol implementation Configuration|+ Client |+Static|-Config.js//WebSocket Client Configuration|-index.html//Login Interface|-main.html//chat room main interface|+ data//Run data|+Log //Swoole Logs and Webim logs|+ SRC//Webim class file storage directory|+ Store |-file.php//Default memory Tmpfs file System (LINUX/DEV/SHM) to store the data, if not Linux, manually modify the $shm_dir|-redis.php//Store chat data in Redis|-server.php//Inherit the class that implements WebSocket, complete certain business functions|+ Vendor//Dependency Package directory

    2.Socket Server and Socket client communication data format

    such as: Login

    Client sends data

    { "cmd" : "login" , "name" : "xdy" , "avatar" : "http://tp3.sinaimg.cn/1586005914/50/5649388281/1" }

    Server Response Login

    { "cmd" : "login" , "fd" :  "to" , "name" : "xdy" , "avatar" : "http://tp3.sinaimg.cn/1586005914/50/5649388281/1" }

    You can see the Cmd property, which is specified by the client and the server when it is sent, primarily for the client or server callback handler function.

    3. The relationship between several agreements or services that need to be cleared

    HTTP protocol: Hypertext Transfer Protocol. Simplex communication, waiting for the client to respond after the request.

    WebSocket protocol: HTML5 is a new protocol that implements full-duplex communication between the browser and the server. Both the server port and the client can pull data.

    Web server: You can use Swoole-based app server as a Web server in this project, or you can use traditional Nginx/apache as a Web server

    Socket server: This project is a server in which the browser's WebSocket client is connected, and Swoole_framework has a PHP version that implements the WebSocket protocol.

    WebSocket Client: Browsers that implement HTML5 support WebSocket objects, such as implementations that provide a flash version in this project are not supported.

Web Instant Chat with Php+swoole: Phpwebim

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.