multichat-ts 0.0.85 → 0.0.87

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,16 @@
1
1
  import Pusher from 'pusher-js';
2
2
  import { type BadgeURLsByNameOrCount, type EmoteURLsByName, type Message } from './index.js';
3
+ type ConnectionState = 'initialized' | 'connecting' | 'connected' | 'unavailable' | 'failed';
4
+ type ConnectionStateEvent = {
5
+ previous: ConnectionState;
6
+ current: ConnectionState;
7
+ };
3
8
  type EventCallbackFunctions = {
4
9
  message: (message: Message) => unknown;
5
10
  subscription: (data: ChatSubscriptionEvent) => unknown;
6
11
  gifted: (data: ChatGiftedEvent) => unknown;
7
12
  raw_message: (message: ChatMessageEvent) => unknown;
13
+ connection_state_changed: (state: ConnectionStateEvent) => unknown;
8
14
  };
9
15
  type EventNames = keyof EventCallbackFunctions;
10
16
  export declare class KickPusher {
@@ -67,10 +67,39 @@ export class KickPusher {
67
67
  });
68
68
  this.socket
69
69
  .subscribe(`chatrooms.${channel_response.chatroom.id}.v2`)
70
- .bind('pusher:subscription_succeeded', () => this.onSubscriptionSuccess())
70
+ .bind('pusher:subscription_succeeded', () => this.onSubscriptionSuccess('pusher:subscription_succeeded'))
71
71
  .bind('App\\Events\\ChatMessageEvent', (data) => this.onChatMessage(data))
72
72
  .bind('App\\Events\\SubscriptionEvent', (data) => this.onChatSubscription(data))
73
73
  .bind('App\\Events\\GiftedSubscriptionsEvent', (data) => this.onChatGifted(data));
74
+ this.socket.connection.bind('state_change', (state) => {
75
+ switch (state.current) {
76
+ case 'connected': {
77
+ this.isConnected = true;
78
+ console.log(`Connected to Kick Pusher (${this.channel_name})!`);
79
+ break;
80
+ }
81
+ case 'connecting': {
82
+ this.isConnected = false;
83
+ console.log(`Connecting to Kick Pusher (${this.channel_name})...`);
84
+ break;
85
+ }
86
+ case 'failed': {
87
+ this.isConnected = false;
88
+ console.log(`Failed to connect to Kick Pusher (${this.channel_name})`);
89
+ break;
90
+ }
91
+ case 'unavailable': {
92
+ this.isConnected = false;
93
+ console.log(`Disconnected from Kick Pusher (${this.channel_name})`);
94
+ break;
95
+ }
96
+ default: {
97
+ this.isConnected = false;
98
+ break;
99
+ }
100
+ }
101
+ this.public_listeners.connection_state_changed?.(state);
102
+ });
74
103
  }
75
104
  disconnect() {
76
105
  this.socket?.disconnect();
@@ -79,8 +108,8 @@ export class KickPusher {
79
108
  on(event_name, callback_fn) {
80
109
  this.public_listeners[event_name] = callback_fn;
81
110
  }
82
- onSubscriptionSuccess() {
83
- console.log(`Connected to Kick Pusher (${this.channel_name})`);
111
+ onSubscriptionSuccess(channel) {
112
+ console.log(`Subscribed to Channel on Kick Pusher (${channel})`);
84
113
  }
85
114
  onChatSubscription(data) {
86
115
  this.public_listeners.subscription?.(data);
@@ -1,10 +1,16 @@
1
1
  import Pusher from 'pusher-js/worker';
2
2
  import { type BadgeURLsByNameOrCount, type EmoteURLsByName, type Message } from './index.js';
3
+ type ConnectionState = 'initialized' | 'connecting' | 'connected' | 'unavailable' | 'failed';
4
+ type ConnectionStateEvent = {
5
+ previous: ConnectionState;
6
+ current: ConnectionState;
7
+ };
3
8
  type EventCallbackFunctions = {
4
9
  message: (message: Message) => unknown;
5
10
  subscription: (data: ChatSubscriptionEvent) => unknown;
6
11
  gifted: (data: ChatGiftedEvent) => unknown;
7
12
  raw_message: (message: ChatMessageEvent) => unknown;
13
+ connection_state_changed: (state: ConnectionStateEvent) => unknown;
8
14
  };
9
15
  type EventNames = keyof EventCallbackFunctions;
10
16
  export declare class KickPusher {
@@ -67,10 +67,39 @@ export class KickPusher {
67
67
  });
68
68
  this.socket
69
69
  .subscribe(`chatrooms.${channel_response.chatroom.id}.v2`)
70
- .bind('pusher:subscription_succeeded', () => this.onSubscriptionSuccess())
70
+ .bind('pusher:subscription_succeeded', () => this.onSubscriptionSuccess('pusher:subscription_succeeded'))
71
71
  .bind('App\\Events\\ChatMessageEvent', (data) => this.onChatMessage(data))
72
72
  .bind('App\\Events\\SubscriptionEvent', (data) => this.onChatSubscription(data))
73
73
  .bind('App\\Events\\GiftedSubscriptionsEvent', (data) => this.onChatGifted(data));
74
+ this.socket.connection.bind('state_change', (state) => {
75
+ switch (state.current) {
76
+ case 'connected': {
77
+ this.isConnected = true;
78
+ console.log(`Connected to Kick Pusher (${this.channel_name})!`);
79
+ break;
80
+ }
81
+ case 'connecting': {
82
+ this.isConnected = false;
83
+ console.log(`Connecting to Kick Pusher (${this.channel_name})...`);
84
+ break;
85
+ }
86
+ case 'failed': {
87
+ this.isConnected = false;
88
+ console.log(`Failed to connect to Kick Pusher (${this.channel_name})`);
89
+ break;
90
+ }
91
+ case 'unavailable': {
92
+ this.isConnected = false;
93
+ console.log(`Disconnected from Kick Pusher (${this.channel_name})`);
94
+ break;
95
+ }
96
+ default: {
97
+ this.isConnected = false;
98
+ break;
99
+ }
100
+ }
101
+ this.public_listeners.connection_state_changed?.(state);
102
+ });
74
103
  }
75
104
  disconnect() {
76
105
  this.socket?.disconnect();
@@ -79,8 +108,8 @@ export class KickPusher {
79
108
  on(event_name, callback_fn) {
80
109
  this.public_listeners[event_name] = callback_fn;
81
110
  }
82
- onSubscriptionSuccess() {
83
- console.log(`Connected to Kick Pusher (${this.channel_name})`);
111
+ onSubscriptionSuccess(channel) {
112
+ console.log(`Subscribed to Channel on Kick Pusher (${channel})`);
84
113
  }
85
114
  onChatSubscription(data) {
86
115
  this.public_listeners.subscription?.(data);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "multichat-ts",
3
- "version": "0.0.85",
3
+ "version": "0.0.87",
4
4
  "type": "module",
5
5
  "description": "Receive type-safe realtime events for chat-related messages on multiple platforms (Twitch, Kick)",
6
6
  "repository": {
@@ -7,11 +7,15 @@ import {
7
7
  type Message,
8
8
  } from './index.js';
9
9
 
10
+ type ConnectionState = 'initialized' | 'connecting' | 'connected' | 'unavailable' | 'failed';
11
+ type ConnectionStateEvent = { previous: ConnectionState; current: ConnectionState };
12
+
10
13
  type EventCallbackFunctions = {
11
14
  message: (message: Message) => unknown;
12
15
  subscription: (data: ChatSubscriptionEvent) => unknown;
13
16
  gifted: (data: ChatGiftedEvent) => unknown;
14
17
  raw_message: (message: ChatMessageEvent) => unknown;
18
+ connection_state_changed: (state: ConnectionStateEvent) => unknown;
15
19
  };
16
20
 
17
21
  type EventNames = keyof EventCallbackFunctions;
@@ -119,7 +123,9 @@ export class KickPusher {
119
123
  */
120
124
  this.socket
121
125
  .subscribe(`chatrooms.${channel_response.chatroom.id}.v2`)
122
- .bind('pusher:subscription_succeeded', () => this.onSubscriptionSuccess())
126
+ .bind('pusher:subscription_succeeded', () =>
127
+ this.onSubscriptionSuccess('pusher:subscription_succeeded'),
128
+ )
123
129
  .bind('App\\Events\\ChatMessageEvent', (data: ChatMessageEvent) => this.onChatMessage(data))
124
130
  .bind('App\\Events\\SubscriptionEvent', (data: ChatSubscriptionEvent) =>
125
131
  this.onChatSubscription(data),
@@ -128,13 +134,35 @@ export class KickPusher {
128
134
  this.onChatGifted(data),
129
135
  );
130
136
 
131
- /*
132
- {"event":"App\\Events\\ChatMessageSentEvent","data":"{\"message\":{\"id\":\"e676ab50-d59c-43ae-b7cd-2b60c5b89c08\",\"message\":null,\"type\":\"info\",\"replied_to\":null,\"is_info\":null,\"link_preview\":null,\"chatroom_id\":5755510,\"role\":\"user\",\"created_at\":1736022879,\"action\":\"gift\",\"optional_message\":null,\"months_subscribed\":null,\"subscriptions_count\":5,\"giftedUsers\":[{\"username\":\"lovesxb1\",\"monthsSubscribed\":1},{\"username\":\"O7no\",\"monthsSubscribed\":1},{\"username\":\"viiRayan\",\"monthsSubscribed\":1},{\"username\":\"Shabib_3005\",\"monthsSubscribed\":1},{\"username\":\"Khadielja\",\"monthsSubscribed\":1}]},\"user\":{\"id\":34844645,\"username\":\"Jana_ai\",\"role\":\"user\",\"isSuperAdmin\":null,\"profile_thumb\":\"https:\\/\\/kick-files-prod.s3.us-west-2.amazonaws.com\\/images\\/user\\/34844645\\/profile_image\\/conversion\\/90b8d01a-acda-4cdb-896b-8ce6d40b6767-thumb.webp?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAS3MDRZGPDOOAYROR%2F20250104%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20250104T203439Z&X-Amz-SignedHeaders=host&X-Amz-Expires=300&X-Amz-Signature=ac0e479335c68c7be15861e9f78866410d64fa1627fc87433365f110901243c0\",\"verified\":false,\"follower_badges\":[],\"is_subscribed\":null,\"is_founder\":false,\"months_subscribed\":null,\"quantity_gifted\":0}}","channel":"chatrooms.5755510"}
133
- */
134
-
135
- /*
136
- {"event":"App\\Events\\LuckyUsersWhoGotGiftSubscriptionsEvent","data":"{\"channel\":{\"id\":5789932,\"user_id\":5882384,\"slug\":\"sulaiman\",\"is_banned\":false,\"playback_url\":\"https:\\/\\/fa723fc1b171.us-west-2.playback.live-video.net\\/api\\/video\\/v1\\/us-west-2.196233775518.channel.L29JBfLzKok0.m3u8\",\"name_updated_at\":null,\"vod_enabled\":true,\"subscription_enabled\":true,\"is_affiliate\":false,\"can_host\":true,\"chatroom\":{\"id\":5755510,\"chatable_type\":\"App\\\\Models\\\\Channel\",\"channel_id\":5789932,\"created_at\":\"2023-06-08T08:24:23.000000Z\",\"updated_at\":\"2024-12-11T07:20:32.000000Z\",\"chat_mode_old\":\"public\",\"chat_mode\":\"public\",\"slow_mode\":false,\"chatable_id\":5789932,\"followers_mode\":true,\"subscribers_mode\":false,\"emotes_mode\":false,\"message_interval\":6,\"following_min_duration\":0}},\"usernames\":[\"lovesxb1\",\"O7no\",\"viiRayan\",\"Shabib_3005\",\"Khadielja\"],\"gifter_username\":\"Jana_ai\"}","channel":"channel.5789932"}
137
- */
137
+ this.socket.connection.bind('state_change', (state: ConnectionStateEvent) => {
138
+ switch (state.current) {
139
+ case 'connected': {
140
+ this.isConnected = true;
141
+ console.log(`Connected to Kick Pusher (${this.channel_name})!`);
142
+ break;
143
+ }
144
+ case 'connecting': {
145
+ this.isConnected = false;
146
+ console.log(`Connecting to Kick Pusher (${this.channel_name})...`);
147
+ break;
148
+ }
149
+ case 'failed': {
150
+ this.isConnected = false;
151
+ console.log(`Failed to connect to Kick Pusher (${this.channel_name})`);
152
+ break;
153
+ }
154
+ case 'unavailable': {
155
+ this.isConnected = false;
156
+ console.log(`Disconnected from Kick Pusher (${this.channel_name})`);
157
+ break;
158
+ }
159
+ default: {
160
+ this.isConnected = false;
161
+ break;
162
+ }
163
+ }
164
+ this.public_listeners.connection_state_changed?.(state);
165
+ });
138
166
  }
139
167
 
140
168
  public disconnect() {
@@ -149,8 +177,8 @@ export class KickPusher {
149
177
  this.public_listeners[event_name] = callback_fn;
150
178
  }
151
179
 
152
- private onSubscriptionSuccess() {
153
- console.log(`Connected to Kick Pusher (${this.channel_name})`);
180
+ private onSubscriptionSuccess(channel: string) {
181
+ console.log(`Subscribed to Channel on Kick Pusher (${channel})`);
154
182
  }
155
183
 
156
184
  private onChatSubscription(data: ChatSubscriptionEvent) {
@@ -7,11 +7,15 @@ import {
7
7
  type Message,
8
8
  } from './index.js';
9
9
 
10
+ type ConnectionState = 'initialized' | 'connecting' | 'connected' | 'unavailable' | 'failed';
11
+ type ConnectionStateEvent = { previous: ConnectionState; current: ConnectionState };
12
+
10
13
  type EventCallbackFunctions = {
11
14
  message: (message: Message) => unknown;
12
15
  subscription: (data: ChatSubscriptionEvent) => unknown;
13
16
  gifted: (data: ChatGiftedEvent) => unknown;
14
17
  raw_message: (message: ChatMessageEvent) => unknown;
18
+ connection_state_changed: (state: ConnectionStateEvent) => unknown;
15
19
  };
16
20
 
17
21
  type EventNames = keyof EventCallbackFunctions;
@@ -119,7 +123,9 @@ export class KickPusher {
119
123
  */
120
124
  this.socket
121
125
  .subscribe(`chatrooms.${channel_response.chatroom.id}.v2`)
122
- .bind('pusher:subscription_succeeded', () => this.onSubscriptionSuccess())
126
+ .bind('pusher:subscription_succeeded', () =>
127
+ this.onSubscriptionSuccess('pusher:subscription_succeeded'),
128
+ )
123
129
  .bind('App\\Events\\ChatMessageEvent', (data: ChatMessageEvent) => this.onChatMessage(data))
124
130
  .bind('App\\Events\\SubscriptionEvent', (data: ChatSubscriptionEvent) =>
125
131
  this.onChatSubscription(data),
@@ -128,13 +134,35 @@ export class KickPusher {
128
134
  this.onChatGifted(data),
129
135
  );
130
136
 
131
- /*
132
- {"event":"App\\Events\\ChatMessageSentEvent","data":"{\"message\":{\"id\":\"e676ab50-d59c-43ae-b7cd-2b60c5b89c08\",\"message\":null,\"type\":\"info\",\"replied_to\":null,\"is_info\":null,\"link_preview\":null,\"chatroom_id\":5755510,\"role\":\"user\",\"created_at\":1736022879,\"action\":\"gift\",\"optional_message\":null,\"months_subscribed\":null,\"subscriptions_count\":5,\"giftedUsers\":[{\"username\":\"lovesxb1\",\"monthsSubscribed\":1},{\"username\":\"O7no\",\"monthsSubscribed\":1},{\"username\":\"viiRayan\",\"monthsSubscribed\":1},{\"username\":\"Shabib_3005\",\"monthsSubscribed\":1},{\"username\":\"Khadielja\",\"monthsSubscribed\":1}]},\"user\":{\"id\":34844645,\"username\":\"Jana_ai\",\"role\":\"user\",\"isSuperAdmin\":null,\"profile_thumb\":\"https:\\/\\/kick-files-prod.s3.us-west-2.amazonaws.com\\/images\\/user\\/34844645\\/profile_image\\/conversion\\/90b8d01a-acda-4cdb-896b-8ce6d40b6767-thumb.webp?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAS3MDRZGPDOOAYROR%2F20250104%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20250104T203439Z&X-Amz-SignedHeaders=host&X-Amz-Expires=300&X-Amz-Signature=ac0e479335c68c7be15861e9f78866410d64fa1627fc87433365f110901243c0\",\"verified\":false,\"follower_badges\":[],\"is_subscribed\":null,\"is_founder\":false,\"months_subscribed\":null,\"quantity_gifted\":0}}","channel":"chatrooms.5755510"}
133
- */
134
-
135
- /*
136
- {"event":"App\\Events\\LuckyUsersWhoGotGiftSubscriptionsEvent","data":"{\"channel\":{\"id\":5789932,\"user_id\":5882384,\"slug\":\"sulaiman\",\"is_banned\":false,\"playback_url\":\"https:\\/\\/fa723fc1b171.us-west-2.playback.live-video.net\\/api\\/video\\/v1\\/us-west-2.196233775518.channel.L29JBfLzKok0.m3u8\",\"name_updated_at\":null,\"vod_enabled\":true,\"subscription_enabled\":true,\"is_affiliate\":false,\"can_host\":true,\"chatroom\":{\"id\":5755510,\"chatable_type\":\"App\\\\Models\\\\Channel\",\"channel_id\":5789932,\"created_at\":\"2023-06-08T08:24:23.000000Z\",\"updated_at\":\"2024-12-11T07:20:32.000000Z\",\"chat_mode_old\":\"public\",\"chat_mode\":\"public\",\"slow_mode\":false,\"chatable_id\":5789932,\"followers_mode\":true,\"subscribers_mode\":false,\"emotes_mode\":false,\"message_interval\":6,\"following_min_duration\":0}},\"usernames\":[\"lovesxb1\",\"O7no\",\"viiRayan\",\"Shabib_3005\",\"Khadielja\"],\"gifter_username\":\"Jana_ai\"}","channel":"channel.5789932"}
137
- */
137
+ this.socket.connection.bind('state_change', (state: ConnectionStateEvent) => {
138
+ switch (state.current) {
139
+ case 'connected': {
140
+ this.isConnected = true;
141
+ console.log(`Connected to Kick Pusher (${this.channel_name})!`);
142
+ break;
143
+ }
144
+ case 'connecting': {
145
+ this.isConnected = false;
146
+ console.log(`Connecting to Kick Pusher (${this.channel_name})...`);
147
+ break;
148
+ }
149
+ case 'failed': {
150
+ this.isConnected = false;
151
+ console.log(`Failed to connect to Kick Pusher (${this.channel_name})`);
152
+ break;
153
+ }
154
+ case 'unavailable': {
155
+ this.isConnected = false;
156
+ console.log(`Disconnected from Kick Pusher (${this.channel_name})`);
157
+ break;
158
+ }
159
+ default: {
160
+ this.isConnected = false;
161
+ break;
162
+ }
163
+ }
164
+ this.public_listeners.connection_state_changed?.(state);
165
+ });
138
166
  }
139
167
 
140
168
  public disconnect() {
@@ -149,8 +177,8 @@ export class KickPusher {
149
177
  this.public_listeners[event_name] = callback_fn;
150
178
  }
151
179
 
152
- private onSubscriptionSuccess() {
153
- console.log(`Connected to Kick Pusher (${this.channel_name})`);
180
+ private onSubscriptionSuccess(channel: string) {
181
+ console.log(`Subscribed to Channel on Kick Pusher (${channel})`);
154
182
  }
155
183
 
156
184
  private onChatSubscription(data: ChatSubscriptionEvent) {