xlabs/notificationsbundle

实时通知包

安装: 502

依赖项: 1

建议者: 0

安全: 0

类型:symfony-bundle

1.0.10 2022-09-27 08:55 UTC

This package is auto-updated.

Last update: 2024-09-27 13:55:16 UTC


README

一个类似Redis的引擎。

安装

通过composer安装

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

在你的AppKernel中

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

路由

追加到主路由文件

# app/config/routing.yml

x_labs_notifications:
    resource: "@XLabsNotificationsBundle/Resources/config/routing.yml"
    prefix:   /

配置示例

以下显示默认值

# app/config/config.yml
  
x_labs_notifications:
    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:notifications'
    settings:
        message_ttl: <expiration_period_in_hours> | false
        socket_io_client_reconnect_atempts: <number_attempts> | false(default - for unlimited attempts)

确保更新所有资源

php bin/console assets:install --symlink

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

php bin/console xlabs_notifications:create:server

重要:确保在设置项目配置后运行此命令。如果配置发生变化,您将需要重新生成服务器。

在 "web/notifications/" 下安装NodeJS依赖项

nvm install 0.10.45
nvm use 0.10.45
npm install

用法

在模板的结束标签之前追加此代码

{{ render(controller('XLabsNotificationsBundle:Notifications:loader')) }}

启动NodeJS服务器

node web/notifications/server.js

渲染用户通知

在模板的任何位置追加此代码

{% include 'XLabsNotificationsBundle:Frontend:notifications.html.twig' with {
    'max_results': <num_results>
} %}

将自动添加Ajax分页。

如果您想以自己的方式渲染通知,通过创建来覆盖模板

app/Resources/XLabsNotificationsBundle/views/Frontend/notification.html.twig

要覆盖“无结果”模板

app/Resources/XLabsNotificationsBundle/views/Frontend/no_results.html.twig

要移除(Ajax)一个通知,向您的DOM元素添加以下属性

'data-xlabs-notifications-remove="<notification_id>"'

要移除(Ajax)所有通知,向您的DOM元素添加以下属性

'data-xlabs-notifications-remove="all"'

要标记为已读(Ajax)一个通知,向您的DOM元素添加以下属性

'data-xlabs-notifications-markAsRead="<notification_id>"'

您也可以通过添加以下属性为上述所有操作定义自定义JS回调函数,以便在操作后执行

'data-xlabs-callback="YOUR_JS_FUNCTION"'

通知清理

强烈建议您创建一个每日cronjob,从用户通知中删除所有自动过期的项目。为此

$xlabs_storage = $container->get('xlabs_notifications_storage');
foreach($users as $user)
{
    $xlabs_storage->clearExpiredUserNotifications($user->getId())
}

要求

Node js连接通过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>;
    }
}