xlabs/chatbundle

实时聊天组件

安装: 102

依赖: 0

建议: 0

安全: 0

类型:symfony-bundle

1.0.22 2020-04-29 14:59 UTC

README

一个类似 Redis 的引擎。

安装

通过 composer 安装

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

此组件依赖于 "xlabs/rabbitmqbundle"。请确保也设置好它。

在你的 AppKernel 中

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

路由

追加到主路由文件

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

配置示例

以下显示默认值

# 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']

此外,如果你在 doctrine 的 orm 配置部分设置了 'resolve_target_entities',你需要添加以下内容

# app/config/config.yml

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

确保你更新了所有资源

php app/console assets:install --symlink

运行命令以创建 NodeJS 服务器文件

php app/console xlabs_chat:create:server

在 "web/chat/" 下安装 NodeJS 依赖

npm install

额外功能

该组件包含一个服务,可以根据某些标准返回 user_ids。例如,查看 'xlabs_chat_user_manager' 服务及其 'searchUsers' 方法,如果你想列出当前活跃的聊天用户。

事件

为了使聊天知道在线/离线用户,请确保创建登录/注销监听器,并添加以下行

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);
    }
}

用法

启动 NodeJS 服务器

node web/chat/server.js

启动 RabbitMQ 消费者,将消息存储在数据库中

php app/console xlabs_chat:message:store

将以下命令添加到你的 crontab 中,以便删除过期的消息

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

如果你想有一个小的新消息通知器,可以在父模板的 <\/body> 标签前添加以下内容:{{ render(controller('XLabsChatBundle:Chat:loader')) }}

需求

确保 doctrine 有以下配置设置

doctrine:
    dbal:
        ...
        charset:  UTF8MB4

NodeJS 连接通过 nginx 反向代理进行。请确保设置 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>;
    }
}

为了防止服务器过载,你可以使用 crontab 调度(计划)此脚本以重启 NodeJS 聊天服务器实例

#!/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
"