定制 xlabs/chatbundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xlabs/chatbundle

Composer 安装命令:

composer require xlabs/chatbundle

包简介

Live chat bundle

README 文档

README

A redis driven like engine.

Installation

Install through composer:

php -d memory_limit=-1 composer.phar require xlabs/chatbundle

This bundle depends on "xlabs/rabbitmqbundle". Make sure to set it up too.

In your AppKernel

public function registerbundles()
{
    return [
    	...
    	...
    	new XLabs\ChatBundle\XLabsChatBundle(),
    ];
}

Routing

Append to main routing file:

# app/config/routing.yml
  
x_labs_chat:
    resource: .
    type: xlabs_chat_routing

Configuration sample

Default values are shown below:

# app/config/config.yml

x_labs_rabbit_mq:
    ...
  
x_labs_chat:
    user_entity: YourBundle\Entity\YourFOSUserExtendedEntity
    user_entity_mappings:
        id: <your_user_entity_id_fieldname>
        username: <your_user_entity_usernam_fieldname>
        avatar: <your_user_entity_avatar_fieldname>
    url: /chat
    nodejs_settings:
        host: your.host.com
        port: 3026
        schema: https
        ssl_key: /etc/nginx/.../your.host.com.key
        ssl_cert: /etc/nginx/.../your.host.com.crt
        ssl_bundle: /etc/nginx/.../your.host.com.(bundle | crt)
    redis_settings:
        host: 192.168.5.23
        port: 6379
        database_id: 20
        _key_namespace: 'your:namespace:chat'
    rabbitmq_settings:
        queue_prefix: 'your_prefix' (queue will be named by this prefix followed by "_chat"); needs to match the one set in x_labs_rabbit_mq bundle config
    uploads:
        folder: your/upload/folder/inside/web/folder
        allowed_extensions: ['jpg', 'jpeg']
        max_file_size: 1048576 (in bytes)
    settings:
        message_ttl: <expiration_period_in_hours> | false
        images_prefix: 'https://cdn.static.stiffia.com/' (for avatar and uploaded images)
        report_to: ['xavi.mateos@manicamedia.com']

Also, if you have 'resolve_target_entities' set in doctrine´s orm config section, you will need to add the following:

# app/config/config.yml

doctrine:
    ...
    orm:
        ...
        resolve_target_entities:
            ...
            XLabs\ChatBundle\Model\XLabsChatUserInterface: YourBundle\Entity\YourFOSUserExtendedEntity

Make sure you update all assets:

php app/console assets:install --symlink

Run command to create NodeJS server file:

php app/console xlabs_chat:create:server

Install NodeJS dependencies under "web/chat/":

npm install

Extra functionalities

There´s a service included in the bundle which will return user_ids based on some cryteria. Have a look at 'xlabs_chat_user_manager' service and its method 'searchUsers', for instance, if you want to list the current active chat users.

Events

In order for the chat to know about online/offline users, make sure to create login/logout listeners, and add the following lines:

namespace YourBundle\LoginEventListener;

use XLabs\ChatBundle\Event\XLabsChatUserOnline;

class MyLoginListener extends Event
{
    public function yourCustomMethod(Event $event)
    {
        ...
    
        $event = new XLabsChatUserOnline(array(
            'user_id' => $user->getId()
        ));
        $this->container->get('event_dispatcher')->dispatch(XLabsChatUserOnline::NAME, $event);
    }
}
namespace YourBundle\LogoutEventListener;

use XLabs\ChatBundle\Event\XLabsChatUserOffline;

class MyLogoutListener extends Event
{
    public function yourCustomMethod(Event $event)
    {
        ...
    
        $event = new XLabsChatUserOffline(array(
            'user_id' => $user->getId()
        ));
        $this->container->get('event_dispatcher')->dispatch(XLabsChatUserOffline::NAME, $event);
    }
}

Usage

Launch the NodeJS server:

node web/chat/server.js

Launch RabbitMQ consumer that stores messages in DB:

php app/console xlabs_chat:message:store

Add the following command in your crontab in order to delete expired messages:

0 1 * * * php app/console xlabs_chat:messages:expire

You can add the following right before the <\/body> tag of you parent template if you want to have a small new message notifier: {{ render(controller('XLabsChatBundle:Chat:loader')) }}

Requirements

Make sure that doctrine has the following config setting:

doctrine:
    dbal:
        ...
        charset:  UTF8MB4

The node js connection is made through a nginx reverse proxy. Make sure to set a nginx vhost:

server {
    listen 443 ssl;

    server_name <x_labs_chat.nodejs_settings.host>;

    ## SSL settings
    ssl on;
    ssl_certificate <x_labs_chat.nodejs_settings.ssl_cert>;
    ssl_certificate_key <x_labs_chat.nodejs_settings.ssl_key>;

    ## SSL caching/optimization
    ssl_protocols        SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers RC4:HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    keepalive_timeout    60;
    ssl_session_cache    shared:SSL:10m;
    ssl_session_timeout  10m;

	location / {
        #proxy_set_header 'Access-Control-Allow-Origin' '*';

        #proxy_set_header X-Real-IP $remote_addr;
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header Host $http_host;
        #proxy_set_header X-NginX-Proxy true;
        #proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

        proxy_pass https://<your_internal_nodejs_server_ip>:<x_labs_chat.nodejs_settings.port>;
    }
}

In order to prevent the server to overload, you can schedule (crontab) a NodeJS chat server instance restart with this script:

#!/bin/bash

# Restart chat
# kill node instance and screen
ps -ef | grep <path_to_your_app>/web/chat/server.js | grep -v grep | awk '{print $2}' | xargs kill && screen -S <screen_name> -X quit
# run node instance in screen
screen -dmS <screen_name> sh && screen -S <screen_name> -X stuff "node <path_to_your_app>/web/chat/server.js
"

xlabs/chatbundle 适用场景与选型建议

xlabs/chatbundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 107 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「xlabs chat live bundle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 xlabs/chatbundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 xlabs/chatbundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 xlabs/chatbundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 107
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-19