Thư viện máy chủ
Laravel thời gian thực với WebSocket Laravel Broadcasting trên máy chủ và Laravel Echo trên trình duyệt, không cần sửa mã.
Đăng nhập để xem các đoạn mã này với host cụm của riêng bạn. Đăng nhập
Đang dùng một AI agent lập trình? Sao chép prompt cho agent ↓
Cài đặt composer require pusher/pusher-php-server
npm install --save-dev laravel-echo pusher-jsĐặt thông tin xác thực của ứng dụng BROADCAST_CONNECTION =pusher
PUSHER_APP_ID =YOUR_APP_ID
PUSHER_APP_KEY =YOUR_APP_KEY
PUSHER_APP_SECRET =YOUR_APP_SECRET
PUSHER_HOST =ws.hibira.io
PUSHER_PORT =443
PUSHER_SCHEME =https
PUSHER_APP_CLUSTER =ap
VITE_PUSHER_APP_KEY ="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST ="${PUSHER_HOST}"
VITE_PUSHER_PORT ="${PUSHER_PORT}"
VITE_PUSHER_SCHEME ="${PUSHER_SCHEME}" Cấu hình client
import Echo from 'laravel-echo' ;
import Pusher from 'pusher-js' ;
window .Pusher = Pusher ;
window .Echo = new Echo ({
broadcaster : 'pusher' ,
key : import .meta .env .VITE_PUSHER_APP_KEY ,
cluster : 'ap' ,
wsHost : import .meta .env .VITE_PUSHER_HOST ,
wsPort : import .meta .env .VITE_PUSHER_PORT ,
wssPort : import .meta .env .VITE_PUSHER_PORT ,
forceTLS : import .meta .env .VITE_PUSHER_SCHEME === 'https' ,
enabledTransports : ['ws' , 'wss' ],
});
window .Echo .private ('orders.' + orderId)
.listen ('OrderShipped' , (e ) => console .log (e.order ));Publish một sự kiện
OrderShipped ::dispatch ($order );Prompt cho agent Dán nội dung này vào Claude Code, Cursor, GitHub Copilot hoặc một AI agent lập trình khác, và nó sẽ tích hợp Hibira vào dự án Laravel của bạn với các cài đặt ở trên.
Integrate Hibira realtime messaging into this Laravel project. Hibira is a hosted WebSocket service that speaks the Pusher Channels protocol, so it works with the standard Pusher libraries (here: Broadcasting + Echo, https://laravel.com/docs/broadcasting) pointed at Hibira instead of pusher.com.
Connection details:
- Host: ws.hibira.io
- Port: 443
- TLS: yes (https)
- App ID: YOUR_APP_ID
- App key: YOUR_APP_KEY
- App secret: YOUR_APP_SECRET (server-side only)
What to do:
1. Look at the project first (framework version, package manager, config and env conventions, where similar integrations live) and follow what it already does.
2. If broadcasting isn't installed yet, run `php artisan install:broadcasting` and choose Pusher; otherwise switch the existing connection to pusher.
3. Make sure config/broadcasting.php passes host, port, scheme and useTLS from the PUSHER_* env values into the pusher connection's options, so nothing points at pusher.com.
4. Configure Laravel Echo in resources/js/echo.js (or bootstrap.js) like the reference below, using the VITE_PUSHER_* values.
5. Authorize private and presence channels in routes/channels.php.
6. Broadcast with events that implement ShouldBroadcast (they need a running queue worker) or ShouldBroadcastNow when the project has no queue.
7. Read the credentials and host from environment variables or the project's config, and add placeholder entries to .env.example (or its equivalent). Never commit real values, and never send the app secret to a browser or mobile app.
8. Always pass the host, port and TLS setting above. The library defaults to pusher.com; where it requires a cluster, use "ap" (the host decides where it connects).
9. If YOUR_APP_KEY or YOUR_APP_SECRET above is still a placeholder, keep it as an env placeholder and tell me where to fill it in rather than inventing a value.
10. When you are done, list the files you changed and tell me how to check it works, for example by publishing a test event and watching it arrive.
Reference setup from the Hibira Laravel guide (adapt it to the project's structure):
Install:
```shell
composer require pusher/pusher-php-server
npm install --save-dev laravel-echo pusher-js
```
Set your app credentials:
```ini
BROADCAST_CONNECTION=pusher
PUSHER_APP_ID=YOUR_APP_ID
PUSHER_APP_KEY=YOUR_APP_KEY
PUSHER_APP_SECRET=YOUR_APP_SECRET
PUSHER_HOST=ws.hibira.io
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=ap
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
```
Configure the client:
```js
// resources/js/echo.js
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';
window.Pusher = Pusher;
window.Echo = new Echo({
broadcaster: 'pusher',
key: import.meta.env.VITE_PUSHER_APP_KEY,
cluster: 'ap',
wsHost: import.meta.env.VITE_PUSHER_HOST,
wsPort: import.meta.env.VITE_PUSHER_PORT,
wssPort: import.meta.env.VITE_PUSHER_PORT,
forceTLS: import.meta.env.VITE_PUSHER_SCHEME === 'https',
enabledTransports: ['ws', 'wss'],
});
window.Echo.private('orders.' + orderId)
.listen('OrderShipped', (e) => console.log(e.order));
```
Publish an event:
```php
// Any event that implements ShouldBroadcast
OrderShipped::dispatch($order);
```
Thay YOUR_APP_KEY và các giá trị giữ chỗ khác bằng giá trị từ bảng điều khiển, hoặc để agent giữ chúng dưới dạng biến môi trường để bạn tự điền.
Hoặc kết nối máy chủ MCP của Hibira, và agent của bạn có thể tự tạo ứng dụng và điền thông tin xác thực. Kết nối AI agent lập trình của bạn →
Dùng app key và secret từ bảng điều khiển. Kênh, sự kiện và endpoint xác thực hoạt động y như với Pusher. Tài liệu thư viện ↗