import { WsKit } from "websocketkit";
import Pusher from 'pusher-js';
declare module 'websocketkit' {
interface ChannelRegistry {
'orders': {
type: 'private';
params: {};
events: {
orderShipped: {
orderId: string,
trackId: string,
status: number,
timestamp: string,
}
};
whispers: {
noteAdded: {
orderId: string,
note: string,
}
}
};
}
}
const pusher = new Pusher('TEST', {cluster: 'mt1'});
const wskit = new WsKit(pusher);
// ---cut---
Private channel
Private channels should be used when access to the channel needs to be restricted in some way. In order for a user to subscribe to a private channel permission must be authorized.
Subscribe
Create a subscription to a specific channel. Provide any required parameters for public, private or presence channels.
const = .('orders', { : 'private', : {} })Unsubscribe
Stop receiving events and clear all listeners from the subscription.
.();Events
Listen for an event
Subscribe to a specific event on the channel.
.('orderShipped', () => {
/** Do something with the event **/
})Stop listening for an event
This should only be used when you want to stop listening for a specific event without unsubscribing from the channel; otherwise, calling sub.dispose() is sufficient.
Remove a specific listener using either the returned unsubscribe function or the original callback reference.
Using returned unsubscribe function
const = .('orderShipped', () => {
/** Do something with the event **/
})
// Later
();Notifications
Listen for a notification
Listen for .Illuminate\\Notifications\\Events\\BroadcastNotificationCreated event on a channel.
.(() => {
/** Do something with a notification event **/
});Stop listening for a notification
This should only be used when you want to stop listening for a specific event without unsubscribing from the channel; otherwise, calling sub.dispose() is sufficient.
Using returned unsubscribe function
const = .(() => {
/** Do something with a notification event **/
});
()Client Events
Communicate directly with other subscribers on the channel.
Send a whisper
.('noteAdded', { : 'rBei1FD7egstHQrplQeou', : 'Review required' });Listen for a whisper
This should only be used when you want to stop listening for a specific event without unsubscribing from the channel; otherwise, calling sub.dispose() is sufficient.
.('noteAdded', () => {
/** Do something with a notification event **/
});Stop listening for a whisper
Using returned unsubscribe function
// Using returned unsubscribe function
const = .('noteAdded', () => {
/** Do something with a notification event **/
});
();Internal Events
Subscription succeeded
Triggered when the subscription is successfully established.
.(() => {
/** Do something after successful subscription **/
})Subscription error
Triggered when authorization fails for private or presence channels. Internally, this listens for the pusher:subscription_error event .
.(() => {
/** Handle subscription error **/
})