luizfabianonogueira / sse-notify
强大的实时通知工具,用于通知用户重要事件
Requires
- php: >=8.3
- laravel/framework: >=v10.0.0
README
🚀 推出 Sse-Notify – Laravel 的动态实时通知解决方案!🎉
如果你正在用 Laravel 开发,并寻找一种有效的方式与用户实时交互,那么解决方案就在这里!使用 [Sse-Notify],你可以
💡 实时通知
利用 SSE(服务器端事件) 的强大功能,轻松从 服务器向客户端 发送通知,确保流畅、实时的用户体验!
🔔 多种通知选项
选择各种样式和模式的通知
- 传统的 JavaScript 提示框
- 使用 Bootstrap Notify 的现代通知
- 使用 Bootstrap Toast 的简洁消息
- 使用 SweetAlert2 的视觉上令人惊叹的弹出窗口
- 用于动态内容的 HTML 和 JavaScript 注入
🎯 无限灵活性
支持 动态 HTML 和 JavaScript 注入,可能性几乎是无限的!创建独特且个性化的交互,以满足项目用户的需求。
⚙️ 简单集成
[Sse-Notify] 很容易集成到你的 Laravel 项目中。只需安装包,就可以开始利用 SSE 通信的力量来提升你的用户通知。
👉 你准备好改变与用户的交互方式了吗?
试试 [Sse-Notify],看看如何轻松地将你的通知提升到下一个层次!
许可证:LGPL-3.0-or-later
需求
PHP 8.2 或更高版本
Laravel 10.0 或更高版本
包含 id uuid 列的用户表
活跃的认证系统
数据库中活跃的 uuid 扩展
安装
composer require luizfabianonogueira/sse-notify
安装后,注册服务提供者。\
在 bootstrap/providers.php。
<?php return [ // other providers... LuizFabianoNogueira\SseNotify\SseServiceProvider::class, ];
之后,你必须发布迁移和 js 文件。
php artisan vendor:publish --tag=sse-notify-migrations php artisan vendor:publish --tag=sse-notify-assets
注意:sse.js 文件将被导出到 public/assets/js/sse.js。
如果你使用 Vite 或类似工具,请正确配置 js 以正确加载。
执行迁移以创建存储通知的表。
php artisan migrate
现在我们需要配置通信频道。
在您选择的视图中打开频道,插入以下代码。
@if(Auth::check()) <!-- Include the sse.js file --> <script src="{{ asset('assets/js/sse.js') }}"></script> <script> <!-- Call the SseConnect function passing the route to the connection --> SseConnect('{{ route('sse.connect', ['userId' => Auth::user()->id]) }}'); </script> @endif
测试
要运行用户测试,只需访问以下 URL
yourdomain.com/sse/generateFakeData/{userId}
使用示例
..::|| JavaScript 提示框 ||::..
$sseMessageAlert = new SseEventMessageAlert([ 'message' => 'Notify Alert!', //string 'userId' => {userId}, //uuid ]); $sseMessageAlert->save();
..::|| SweetAlert2 ||::..
$data = [ 'title' => 'Notify title Sweet!', //string 'message' => 'Notify message Sweet!', //string 'type' => SseEnumStyle::STYLE_SUCCESS, //Enum types: STYLE_SUCCESS, STYLE_ERROR, STYLE_WARNING, STYLE_INFO 'id' => time(),// int id unique 'confirm' => false, // if true show confirm button 'userId' => $this->userId //uuid - user to be notified ]; (new SseEventMessageSweet($data))->save();
..::|| Bootstrap Notify ||::..
$data = [ 'title' => 'Notify title notify!', //string 'message' => 'Notify message notify!', //string 'type' => SseEnumStyle::STYLE_SUCCESS, //Enum types: STYLE_SUCCESS, STYLE_ERROR, STYLE_WARNING, STYLE_INFO 'id' => time(), // int id unique 'autoClose' => true, // if true auto close 'userId' => $this->userId //uuid - user to be notified ]; (new SseEventMessageNotify($data))->save();
..::|| Bootstrap TOAST ||::..
$data = [ 'title' => 'Notify title toast!', //string 'message' => 'Notify message toast!', //string 'type' => SseEnumStyle::STYLE_SUCCESS, //Enum types: STYLE_SUCCESS, STYLE_ERROR, STYLE_WARNING, STYLE_INFO 'id' => time(), // int id unique 'autoClose' => true, // if true auto close 'userId' => $this->userId, //uuid - user to be notified 'imgURL' => '/android-chrome-192x192.png', //string - image url [optional] 'linkURL' => 'https://www.google.com', //string - link url [optional] 'linkText' => 'Google' //string - link text [optional] ]; (new SseEventMessageToast($data))->save();
..::|| 注入 HTML ||::..
(new SseEventInjectHtml([ 'html' => '<div class="alert alert-success">Notify message!</div>', //string - html to be injected 'target' => 'boxTestes', //string - target id to inject 'userId' => $this->userId //uuid - user to be notified ]))->save();
..::|| 注入脚本 ||::..
(new SseEventInjectScript([ 'script' => 'console.log("Notify message! - ' . $i . '")', //string - script to be injected 'userId' => $this->userId //uuid - user to be notified ]))->save();
保存后,系统将通知发送给目标用户。
每个通知之间有 3 秒的间隔。
如果没有通知,下一个检查将在 10 秒后进行。
联系 & 支持
📞 电话: +5548991779088
✉️ 电子邮件: luizfabianonogueira@gmail.com
支持我的工作
如果你喜欢这个项目,并想支持我的工作,任何通过 Pix 的捐赠都将非常感激!
请随意使用以下 Pix 密钥之一进行捐赠
💳 电子邮件 Pix 密钥: luizfabianonogueira@gmail.com
📱 电话 Pix 密钥: +55 48 99177-9088
感谢您的支持!