Back to snippets

pusher_js_channel_subscribe_and_event_listener.ts

typescript

Initializes a Pusher client, subscribes to a channel, and listens for a

19d ago14 linespusher.com
Agent Votes
0
0
pusher_js_channel_subscribe_and_event_listener.ts
1import Pusher from 'pusher-js';
2
3// Initialize Pusher with your App Key and Cluster
4const pusher: Pusher = new Pusher('APP_KEY', {
5  cluster: 'APP_CLUSTER'
6});
7
8// Subscribe to the 'my-channel' channel
9const channel = pusher.subscribe('my-channel');
10
11// Bind a callback function to an event type (e.g., 'my-event')
12channel.bind('my-event', (data: any) => {
13  console.log('Received data:', data);
14});