@ceylonitsolutions/pushstream-js 1.0.0
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.
- package/README.md +1 -0
- package/package.json +20 -0
- package/pushstream.js +225 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# pushstream-js
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ceylonitsolutions/pushstream-js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "PushStream JavaScript SDK for real-time messaging",
|
|
5
|
+
"main": "pushstream.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": ["websocket", "realtime", "pusher", "messaging"],
|
|
10
|
+
"author": "Ceylon IT Solutions",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/bkkalana/pushstream-js.git"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {},
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=14.0.0"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/pushstream.js
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
class PushStream {
|
|
2
|
+
constructor(appKey, options = {}) {
|
|
3
|
+
this.appKey = appKey;
|
|
4
|
+
this.wsUrl = options.wsUrl || 'wss://ws.pushstream.ceylonitsolutions.online';
|
|
5
|
+
this.apiUrl = options.apiUrl || 'https://api.pushstream.ceylonitsolutions.online';
|
|
6
|
+
this.ws = null;
|
|
7
|
+
this.socketId = null;
|
|
8
|
+
this.channels = new Map();
|
|
9
|
+
this.reconnectAttempts = 0;
|
|
10
|
+
this.maxReconnectAttempts = 5;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
connect() {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
try {
|
|
16
|
+
this.ws = new WebSocket(this.wsUrl);
|
|
17
|
+
} catch (error) {
|
|
18
|
+
reject(error);
|
|
19
|
+
this.attemptReconnect();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const timeout = setTimeout(() => {
|
|
24
|
+
if (!this.socketId) {
|
|
25
|
+
reject(new Error('Connection timeout'));
|
|
26
|
+
this.attemptReconnect();
|
|
27
|
+
}
|
|
28
|
+
}, 10000);
|
|
29
|
+
|
|
30
|
+
this.ws.onopen = () => {
|
|
31
|
+
console.log('[PushStream] Connected');
|
|
32
|
+
this.reconnectAttempts = 0;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
this.ws.onmessage = (event) => {
|
|
36
|
+
const message = JSON.parse(event.data);
|
|
37
|
+
|
|
38
|
+
if (message.event === 'pusher:connection_established') {
|
|
39
|
+
const data = JSON.parse(message.data);
|
|
40
|
+
this.socketId = data.socket_id;
|
|
41
|
+
clearTimeout(timeout);
|
|
42
|
+
resolve(this.socketId);
|
|
43
|
+
} else if (message.event === 'pusher:error') {
|
|
44
|
+
console.error('[PushStream] Error:', message.data);
|
|
45
|
+
} else {
|
|
46
|
+
this.handleMessage(message);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
this.ws.onclose = () => {
|
|
51
|
+
console.log('[PushStream] Disconnected');
|
|
52
|
+
clearTimeout(timeout);
|
|
53
|
+
this.socketId = null;
|
|
54
|
+
this.attemptReconnect();
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
this.ws.onerror = (error) => {
|
|
58
|
+
console.error('[PushStream] WebSocket error:', error);
|
|
59
|
+
clearTimeout(timeout);
|
|
60
|
+
if (!this.socketId) reject(error);
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
attemptReconnect() {
|
|
66
|
+
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
67
|
+
console.error('[PushStream] Max reconnection attempts reached');
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), 30000);
|
|
72
|
+
this.reconnectAttempts++;
|
|
73
|
+
|
|
74
|
+
console.log(`[PushStream] Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts})`);
|
|
75
|
+
setTimeout(() => this.connect(), delay);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
subscribe(channelName, callbacks = {}) {
|
|
79
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
80
|
+
throw new Error('Not connected');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const channel = new Channel(channelName, this, callbacks);
|
|
84
|
+
this.channels.set(channelName, channel);
|
|
85
|
+
|
|
86
|
+
this.send({
|
|
87
|
+
event: 'pusher:subscribe',
|
|
88
|
+
data: { channel: channelName }
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return channel;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
unsubscribe(channelName) {
|
|
95
|
+
this.send({
|
|
96
|
+
event: 'pusher:unsubscribe',
|
|
97
|
+
data: { channel: channelName }
|
|
98
|
+
});
|
|
99
|
+
this.channels.delete(channelName);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
send(data) {
|
|
103
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
104
|
+
this.ws.send(JSON.stringify(data));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
handleMessage(message) {
|
|
109
|
+
const channel = this.channels.get(message.channel);
|
|
110
|
+
if (channel) {
|
|
111
|
+
channel.handleEvent(message.event, JSON.parse(message.data));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
disconnect() {
|
|
116
|
+
if (this.ws) {
|
|
117
|
+
this.ws.close();
|
|
118
|
+
this.ws = null;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// REST API methods
|
|
123
|
+
async publish(appId, appSecret, channel, event, data) {
|
|
124
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
125
|
+
const body = JSON.stringify({ name: event, channel, data });
|
|
126
|
+
const path = `/api/apps/${appId}/events`;
|
|
127
|
+
const queryString = `auth_timestamp=${timestamp}`;
|
|
128
|
+
const stringToSign = `POST\n${path}\n${queryString}\n${body}`;
|
|
129
|
+
|
|
130
|
+
const signature = await this.hmacSha256(stringToSign, appSecret);
|
|
131
|
+
const authHeader = `${appId}:${signature}`;
|
|
132
|
+
|
|
133
|
+
const response = await fetch(`${this.apiUrl}${path}?${queryString}`, {
|
|
134
|
+
method: 'POST',
|
|
135
|
+
headers: {
|
|
136
|
+
'Authorization': authHeader,
|
|
137
|
+
'Content-Type': 'application/json'
|
|
138
|
+
},
|
|
139
|
+
body
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
if (!response.ok) {
|
|
143
|
+
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return response.json();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async hmacSha256(message, secret) {
|
|
150
|
+
// Node.js
|
|
151
|
+
if (typeof window === 'undefined') {
|
|
152
|
+
const crypto = require('crypto');
|
|
153
|
+
return crypto.createHmac('sha256', secret).update(message).digest('hex');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Browser
|
|
157
|
+
const encoder = new TextEncoder();
|
|
158
|
+
const keyData = encoder.encode(secret);
|
|
159
|
+
const messageData = encoder.encode(message);
|
|
160
|
+
|
|
161
|
+
const key = await crypto.subtle.importKey(
|
|
162
|
+
'raw',
|
|
163
|
+
keyData,
|
|
164
|
+
{ name: 'HMAC', hash: 'SHA-256' },
|
|
165
|
+
false,
|
|
166
|
+
['sign']
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
const signature = await crypto.subtle.sign('HMAC', key, messageData);
|
|
170
|
+
return Array.from(new Uint8Array(signature))
|
|
171
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
172
|
+
.join('');
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
class Channel {
|
|
177
|
+
constructor(name, client, callbacks = {}) {
|
|
178
|
+
this.name = name;
|
|
179
|
+
this.client = client;
|
|
180
|
+
this.callbacks = callbacks;
|
|
181
|
+
this.eventHandlers = new Map();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
bind(event, callback) {
|
|
185
|
+
if (!this.eventHandlers.has(event)) {
|
|
186
|
+
this.eventHandlers.set(event, []);
|
|
187
|
+
}
|
|
188
|
+
this.eventHandlers.get(event).push(callback);
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
unbind(event, callback) {
|
|
193
|
+
if (!this.eventHandlers.has(event)) return;
|
|
194
|
+
|
|
195
|
+
if (callback) {
|
|
196
|
+
const handlers = this.eventHandlers.get(event);
|
|
197
|
+
const index = handlers.indexOf(callback);
|
|
198
|
+
if (index > -1) handlers.splice(index, 1);
|
|
199
|
+
} else {
|
|
200
|
+
this.eventHandlers.delete(event);
|
|
201
|
+
}
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
handleEvent(event, data) {
|
|
206
|
+
const handlers = this.eventHandlers.get(event);
|
|
207
|
+
if (handlers) {
|
|
208
|
+
handlers.forEach(handler => handler(data));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
unsubscribe() {
|
|
213
|
+
this.client.unsubscribe(this.name);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Node.js support
|
|
218
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
219
|
+
module.exports = PushStream;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Browser support
|
|
223
|
+
if (typeof window !== 'undefined') {
|
|
224
|
+
window.PushStream = PushStream;
|
|
225
|
+
}
|