multichat-ts 0.0.85 → 0.0.86

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,31 @@ 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
+ console.log(`Connected to Kick Pusher (${this.channel_name})!`);
78
+ break;
79
+ }
80
+ case 'connecting': {
81
+ console.log(`Connecting to Kick Pusher (${this.channel_name})...`);
82
+ break;
83
+ }
84
+ case 'failed': {
85
+ console.log(`Failed to connect to Kick Pusher (${this.channel_name})`);
86
+ break;
87
+ }
88
+ case 'unavailable': {
89
+ console.log(`Disconnected from Kick Pusher (${this.channel_name})`);
90
+ break;
91
+ }
92
+ }
93
+ this.public_listeners.connection_state_changed?.(state);
94
+ });
74
95
  }
75
96
  disconnect() {
76
97
  this.socket?.disconnect();
@@ -79,8 +100,8 @@ export class KickPusher {
79
100
  on(event_name, callback_fn) {
80
101
  this.public_listeners[event_name] = callback_fn;
81
102
  }
82
- onSubscriptionSuccess() {
83
- console.log(`Connected to Kick Pusher (${this.channel_name})`);
103
+ onSubscriptionSuccess(channel) {
104
+ console.log(`Subscribed to Channel on Kick Pusher (${channel})`);
84
105
  }
85
106
  onChatSubscription(data) {
86
107
  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,31 @@ 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
+ console.log(`Connected to Kick Pusher (${this.channel_name})!`);
78
+ break;
79
+ }
80
+ case 'connecting': {
81
+ console.log(`Connecting to Kick Pusher (${this.channel_name})...`);
82
+ break;
83
+ }
84
+ case 'failed': {
85
+ console.log(`Failed to connect to Kick Pusher (${this.channel_name})`);
86
+ break;
87
+ }
88
+ case 'unavailable': {
89
+ console.log(`Disconnected from Kick Pusher (${this.channel_name})`);
90
+ break;
91
+ }
92
+ }
93
+ this.public_listeners.connection_state_changed?.(state);
94
+ });
74
95
  }
75
96
  disconnect() {
76
97
  this.socket?.disconnect();
@@ -79,8 +100,8 @@ export class KickPusher {
79
100
  on(event_name, callback_fn) {
80
101
  this.public_listeners[event_name] = callback_fn;
81
102
  }
82
- onSubscriptionSuccess() {
83
- console.log(`Connected to Kick Pusher (${this.channel_name})`);
103
+ onSubscriptionSuccess(channel) {
104
+ console.log(`Subscribed to Channel on Kick Pusher (${channel})`);
84
105
  }
85
106
  onChatSubscription(data) {
86
107
  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.86",
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,27 @@ 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
+ console.log(`Connected to Kick Pusher (${this.channel_name})!`);
141
+ break;
142
+ }
143
+ case 'connecting': {
144
+ console.log(`Connecting to Kick Pusher (${this.channel_name})...`);
145
+ break;
146
+ }
147
+ case 'failed': {
148
+ console.log(`Failed to connect to Kick Pusher (${this.channel_name})`);
149
+ break;
150
+ }
151
+ case 'unavailable': {
152
+ console.log(`Disconnected from Kick Pusher (${this.channel_name})`);
153
+ break;
154
+ }
155
+ }
156
+ this.public_listeners.connection_state_changed?.(state);
157
+ });
138
158
  }
139
159
 
140
160
  public disconnect() {
@@ -149,8 +169,8 @@ export class KickPusher {
149
169
  this.public_listeners[event_name] = callback_fn;
150
170
  }
151
171
 
152
- private onSubscriptionSuccess() {
153
- console.log(`Connected to Kick Pusher (${this.channel_name})`);
172
+ private onSubscriptionSuccess(channel: string) {
173
+ console.log(`Subscribed to Channel on Kick Pusher (${channel})`);
154
174
  }
155
175
 
156
176
  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,27 @@ 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
+ console.log(`Connected to Kick Pusher (${this.channel_name})!`);
141
+ break;
142
+ }
143
+ case 'connecting': {
144
+ console.log(`Connecting to Kick Pusher (${this.channel_name})...`);
145
+ break;
146
+ }
147
+ case 'failed': {
148
+ console.log(`Failed to connect to Kick Pusher (${this.channel_name})`);
149
+ break;
150
+ }
151
+ case 'unavailable': {
152
+ console.log(`Disconnected from Kick Pusher (${this.channel_name})`);
153
+ break;
154
+ }
155
+ }
156
+ this.public_listeners.connection_state_changed?.(state);
157
+ });
138
158
  }
139
159
 
140
160
  public disconnect() {
@@ -149,8 +169,8 @@ export class KickPusher {
149
169
  this.public_listeners[event_name] = callback_fn;
150
170
  }
151
171
 
152
- private onSubscriptionSuccess() {
153
- console.log(`Connected to Kick Pusher (${this.channel_name})`);
172
+ private onSubscriptionSuccess(channel: string) {
173
+ console.log(`Subscribed to Channel on Kick Pusher (${channel})`);
154
174
  }
155
175
 
156
176
  private onChatSubscription(data: ChatSubscriptionEvent) {