Thư viện client
JavaScript thời gian thực với WebSocket Trình duyệt, React, Vue và React Native với thư viện pusher-js chính thức.
Đă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 Đăng ký một kênh import Pusher from 'pusher-js' ;
const pusher = new Pusher ('YOUR_APP_KEY' , {
cluster : 'ap' ,
wsHost : 'ws.hibira.io' ,
wsPort : 443 ,
wssPort : 443 ,
forceTLS : true ,
enabledTransports : ['ws' , 'wss' ],
channelAuthorization : { endpoint : '/broadcasting/auth' , transport : 'ajax' },
});
const channel = pusher.subscribe ('orders' );
channel.bind ('order-shipped' , (data ) => {
console .log ('Order shipped' , data);
});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 JavaScript của bạn với các cài đặt ở trên.
Integrate Hibira realtime messaging into this JavaScript project. Hibira is a hosted WebSocket service that speaks the Pusher Channels protocol, so it works with the standard Pusher libraries (here: pusher-js, https://github.com/pusher/pusher-js) pointed at Hibira instead of pusher.com.
Connection details:
- Host: ws.hibira.io
- Port: 443
- TLS: yes (https)
- App key: YOUR_APP_KEY
- No app secret: it belongs on the server, never in this app
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. Create one shared Pusher client module (for example src/lib/realtime.ts) configured like the reference below, and import it wherever the app needs live updates.
3. Read the app key and host from the build tool's public env variables (VITE_, NEXT_PUBLIC_, EXPO_PUBLIC_ or whatever the project already uses).
4. Unbind handlers and unsubscribe when a component unmounts (React useEffect cleanup, Vue onUnmounted, Svelte onDestroy) so connections and listeners don't pile up.
5. If the app uses private or presence channels, point channelAuthorization at the backend route that signs subscriptions; that route uses the app secret and lives on the server only.
6. 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.
7. 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).
8. If YOUR_APP_KEY above is still a placeholder, keep it as an env placeholder and tell me where to fill it in rather than inventing a value.
9. 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 JavaScript guide (adapt it to the project's structure):
Install:
```shell
npm install pusher-js
```
Subscribe to a channel:
```js
import Pusher from 'pusher-js';
const pusher = new Pusher('YOUR_APP_KEY', {
cluster: 'ap',
wsHost: 'ws.hibira.io',
wsPort: 443,
wssPort: 443,
forceTLS: true,
enabledTransports: ['ws', 'wss'],
// Private and presence channels
channelAuthorization: { endpoint: '/broadcasting/auth', transport: 'ajax' },
});
const channel = pusher.subscribe('orders');
channel.bind('order-shipped', (data) => {
console.log('Order shipped', data);
});
```
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 ↗