gfs/notifications
Composer 安装命令:
composer require gfs/notifications
包简介
Symfony GFSNotificationsBundle
关键字:
README 文档
README
Install via composer
composer require gfs/notifications ~v1.0
Add to AppKernel.php
new GFS\NotificationBundle\NotificationBundle(),
2. Create Notification
use GFS\NotificationBundle\Entity\Notification as Base; class Notifications extends Base { /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var User * * @ORM\ManyToOne(targetEntity="User",inversedBy="notifications") * @ORM\JoinColumn(name="user_id", onDelete="cascade") */ private $user; /** * @param UserInterface $user * * @return $this */ public function setUser(UserInterface $user) { $this->user = $user; return $this; } /** * @return User */ public function getUser() { return $this->user; } /** * Get id * * @return int */ public function getId() { return $this->id; } /** * This is important because the server will send your JSON notifications ( json_encode(your notification) ). * You can customize what field you want the server to send to your client. */ public function jsonSerialize() { return [ 'type' => $this->getType(), 'description' => $this->getDescription(), 'checked' => $this->getChecked(), 'checkedAt' => $this->getCheckedAt(), 'createdAt' => $this->getCreateAt(), 'url' => $this->url, 'id' => $this->id, 'userId' => $this->user->getUsername() //This helps the server identify if a specific user is connected and only send notifications to that user. You can use another field for common notifications, for example group notifications. ]; } }
Function jsonSerialize should return an array that must contain the field 'userId', the server checks all connections if they contain this value.
3. Start server
Symfony 2 php app/console server:notification
Symfony 3 php bin/console server:notification
4. Client job
Use any WebSocket you want or any technologies. The most important thing is the URL, it must have GET parameter userId. That parameter will be bind to current connection.
This userId is used when you create a notification, the parameter userId comes from function jsonSerialize.
The GET parameter userId should be identical with the one returned by the function jsonSerialize, so the user receives the notification.
var conn = new WebSocket('ws://yourip:8080?userId='+$scope.username); conn.onopen = function(e) { console.log("Connection established!"); }; conn.onmessage = function(e) { var noitification = JSON.parse(e.data); // handle your notification object here };
5. Create a notification
Create an entity: Notification. After successfully inserting it in the database, a notification will be sent to the server and the server will find the connection that matches userId field from jsonSerialize.
$notification = new Notifications(); $notification->setType('type') ->setDescription('Your Notification here') ->setUser($user) ; $this->get('doctrine.orm.default_entity_manager')->persist($notification); $this->get('doctrine.orm.default_entity_manager')->flush();
Configuration
#config.yml gfs_notifications: host: localhost # ip or DNS where server run default is localhost port: 8080 # port when server want run default is 8080 notification: GFS\NotificationBundle\Notification\Notification # you can implement your own Ratchet\MessageComponentInterface
Default Entity field:
- id
- type ( string, 255 )
- description ( text )
- created_at ( DateTime )
- checked_at ( DateTime, default null )
- checked ( boolean )
- user ( instance of Symfony\Component\Security\Core\User\UserInterface )
Remeber if you want rewrite __contruct don't forget to write:
$this->created_at = new \DateTime('now').
gfs/notifications 适用场景与选型建议
gfs/notifications 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 598 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「User notifications」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gfs/notifications 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gfs/notifications 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gfs/notifications 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova package that adds a notification feed in your Nova app.
Standalone replacement for php's native get_browser() function
Captures outgoing SMS notifications
User component for Yii2
Laravel package to send notifications when some exceptions are thrown.
User Approval Laravel Package
统计信息
- 总下载量: 598
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-21