oorja 1.11.4 → 2.0.1
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 +14 -52
- package/dist/commands/signout.d.ts +1 -1
- package/dist/commands/signout.js +4 -4
- package/dist/commands/teletype/index.d.ts +7 -6
- package/dist/commands/teletype/index.js +49 -47
- package/dist/lib/config.d.ts +7 -10
- package/dist/lib/config.js +29 -49
- package/dist/lib/{surya → connect}/index.d.ts +8 -8
- package/dist/lib/{surya → connect}/index.js +39 -36
- package/dist/lib/{surya → connect}/resources.js +2 -2
- package/dist/lib/{surya → connect}/types.d.ts +1 -3
- package/dist/lib/encryption.d.ts +2 -3
- package/dist/lib/encryption.js +6 -10
- package/dist/lib/oorja/client.d.ts +3 -0
- package/dist/lib/oorja/client.js +52 -0
- package/dist/lib/oorja/index.d.ts +7 -7
- package/dist/lib/oorja/index.js +40 -29
- package/dist/lib/oorja/preflight.d.ts +8 -8
- package/dist/lib/oorja/preflight.js +45 -41
- package/dist/lib/teletype/auxiliary.d.ts +2 -2
- package/dist/lib/teletype/auxiliary.js +5 -5
- package/dist/lib/teletype/index.d.ts +3 -4
- package/dist/lib/teletype/index.js +16 -16
- package/dist/lib/utils.js +5 -5
- package/oclif.manifest.json +11 -10
- package/package.json +22 -18
- package/dist/lib/surya/vendor/phoenix/ajax.d.ts +0 -8
- package/dist/lib/surya/vendor/phoenix/ajax.js +0 -82
- package/dist/lib/surya/vendor/phoenix/channel.d.ts +0 -154
- package/dist/lib/surya/vendor/phoenix/channel.js +0 -308
- package/dist/lib/surya/vendor/phoenix/constants.d.ts +0 -33
- package/dist/lib/surya/vendor/phoenix/constants.js +0 -29
- package/dist/lib/surya/vendor/phoenix/index.d.ts +0 -199
- package/dist/lib/surya/vendor/phoenix/index.js +0 -200
- package/dist/lib/surya/vendor/phoenix/longpoll.d.ts +0 -12
- package/dist/lib/surya/vendor/phoenix/longpoll.js +0 -126
- package/dist/lib/surya/vendor/phoenix/presence.d.ts +0 -44
- package/dist/lib/surya/vendor/phoenix/presence.js +0 -152
- package/dist/lib/surya/vendor/phoenix/push.d.ts +0 -57
- package/dist/lib/surya/vendor/phoenix/push.js +0 -122
- package/dist/lib/surya/vendor/phoenix/serializer.d.ts +0 -53
- package/dist/lib/surya/vendor/phoenix/serializer.js +0 -100
- package/dist/lib/surya/vendor/phoenix/socket.d.ts +0 -222
- package/dist/lib/surya/vendor/phoenix/socket.js +0 -541
- package/dist/lib/surya/vendor/phoenix/timer.d.ts +0 -25
- package/dist/lib/surya/vendor/phoenix/timer.js +0 -40
- package/dist/lib/surya/vendor/phoenix/utils.d.ts +0 -1
- package/dist/lib/surya/vendor/phoenix/utils.js +0 -11
- /package/dist/lib/{surya → connect}/errors.d.ts +0 -0
- /package/dist/lib/{surya → connect}/errors.js +0 -0
- /package/dist/lib/{surya → connect}/resources.d.ts +0 -0
- /package/dist/lib/{surya → connect}/types.js +0 -0
|
@@ -1,308 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
import { closure } from "./utils.js";
|
|
3
|
-
import { CHANNEL_EVENTS, CHANNEL_STATES, } from "./constants.js";
|
|
4
|
-
import Push from "./push.js";
|
|
5
|
-
import Timer from "./timer.js";
|
|
6
|
-
/**
|
|
7
|
-
*
|
|
8
|
-
* @param {string} topic
|
|
9
|
-
* @param {(Object|function)} params
|
|
10
|
-
* @param {Socket} socket
|
|
11
|
-
*/
|
|
12
|
-
export default class Channel {
|
|
13
|
-
constructor(topic, params, socket) {
|
|
14
|
-
this.state = CHANNEL_STATES.closed;
|
|
15
|
-
this.topic = topic;
|
|
16
|
-
this.params = closure(params || {});
|
|
17
|
-
this.socket = socket;
|
|
18
|
-
this.bindings = [];
|
|
19
|
-
this.bindingRef = 0;
|
|
20
|
-
this.timeout = this.socket.timeout;
|
|
21
|
-
this.joinedOnce = false;
|
|
22
|
-
this.joinPush = new Push(this, CHANNEL_EVENTS.join, this.params, this.timeout);
|
|
23
|
-
this.pushBuffer = [];
|
|
24
|
-
this.stateChangeRefs = [];
|
|
25
|
-
this.rejoinTimer = new Timer(() => {
|
|
26
|
-
if (this.socket.isConnected()) {
|
|
27
|
-
this.rejoin();
|
|
28
|
-
}
|
|
29
|
-
}, this.socket.rejoinAfterMs);
|
|
30
|
-
this.stateChangeRefs.push(this.socket.onError(() => this.rejoinTimer.reset()));
|
|
31
|
-
this.stateChangeRefs.push(this.socket.onOpen(() => {
|
|
32
|
-
this.rejoinTimer.reset();
|
|
33
|
-
if (this.isErrored()) {
|
|
34
|
-
this.rejoin();
|
|
35
|
-
}
|
|
36
|
-
}));
|
|
37
|
-
this.joinPush.receive("ok", () => {
|
|
38
|
-
this.state = CHANNEL_STATES.joined;
|
|
39
|
-
this.rejoinTimer.reset();
|
|
40
|
-
this.pushBuffer.forEach(pushEvent => pushEvent.send());
|
|
41
|
-
this.pushBuffer = [];
|
|
42
|
-
});
|
|
43
|
-
this.joinPush.receive("error", () => {
|
|
44
|
-
this.state = CHANNEL_STATES.errored;
|
|
45
|
-
if (this.socket.isConnected()) {
|
|
46
|
-
this.rejoinTimer.scheduleTimeout();
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
this.onClose(() => {
|
|
50
|
-
this.rejoinTimer.reset();
|
|
51
|
-
if (this.socket.hasLogger())
|
|
52
|
-
this.socket.log("channel", `close ${this.topic} ${this.joinRef()}`);
|
|
53
|
-
this.state = CHANNEL_STATES.closed;
|
|
54
|
-
this.socket.remove(this);
|
|
55
|
-
});
|
|
56
|
-
this.onError(reason => {
|
|
57
|
-
if (this.socket.hasLogger())
|
|
58
|
-
this.socket.log("channel", `error ${this.topic}`, reason);
|
|
59
|
-
if (this.isJoining()) {
|
|
60
|
-
this.joinPush.reset();
|
|
61
|
-
}
|
|
62
|
-
this.state = CHANNEL_STATES.errored;
|
|
63
|
-
if (this.socket.isConnected()) {
|
|
64
|
-
this.rejoinTimer.scheduleTimeout();
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
this.joinPush.receive("timeout", () => {
|
|
68
|
-
if (this.socket.hasLogger())
|
|
69
|
-
this.socket.log("channel", `timeout ${this.topic} (${this.joinRef()})`, this.joinPush.timeout);
|
|
70
|
-
let leavePush = new Push(this, CHANNEL_EVENTS.leave, closure({}), this.timeout);
|
|
71
|
-
leavePush.send();
|
|
72
|
-
this.state = CHANNEL_STATES.errored;
|
|
73
|
-
this.joinPush.reset();
|
|
74
|
-
if (this.socket.isConnected()) {
|
|
75
|
-
this.rejoinTimer.scheduleTimeout();
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
this.on(CHANNEL_EVENTS.reply, (payload, ref) => {
|
|
79
|
-
this.trigger(this.replyEventName(ref), payload);
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Join the channel
|
|
84
|
-
* @param {integer} timeout
|
|
85
|
-
* @returns {Push}
|
|
86
|
-
*/
|
|
87
|
-
join(timeout = this.timeout) {
|
|
88
|
-
if (this.joinedOnce) {
|
|
89
|
-
throw new Error("tried to join multiple times. 'join' can only be called a single time per channel instance");
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
this.timeout = timeout;
|
|
93
|
-
this.joinedOnce = true;
|
|
94
|
-
this.rejoin();
|
|
95
|
-
return this.joinPush;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Hook into channel close
|
|
100
|
-
* @param {Function} callback
|
|
101
|
-
*/
|
|
102
|
-
onClose(callback) {
|
|
103
|
-
this.on(CHANNEL_EVENTS.close, callback);
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Hook into channel errors
|
|
107
|
-
* @param {Function} callback
|
|
108
|
-
*/
|
|
109
|
-
onError(callback) {
|
|
110
|
-
return this.on(CHANNEL_EVENTS.error, reason => callback(reason));
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Subscribes on channel events
|
|
114
|
-
*
|
|
115
|
-
* Subscription returns a ref counter, which can be used later to
|
|
116
|
-
* unsubscribe the exact event listener
|
|
117
|
-
*
|
|
118
|
-
* @example
|
|
119
|
-
* const ref1 = channel.on("event", do_stuff)
|
|
120
|
-
* const ref2 = channel.on("event", do_other_stuff)
|
|
121
|
-
* channel.off("event", ref1)
|
|
122
|
-
* // Since unsubscription, do_stuff won't fire,
|
|
123
|
-
* // while do_other_stuff will keep firing on the "event"
|
|
124
|
-
*
|
|
125
|
-
* @param {string} event
|
|
126
|
-
* @param {Function} callback
|
|
127
|
-
* @returns {integer} ref
|
|
128
|
-
*/
|
|
129
|
-
on(event, callback) {
|
|
130
|
-
let ref = this.bindingRef++;
|
|
131
|
-
this.bindings.push({ event, ref, callback });
|
|
132
|
-
return ref;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Unsubscribes off of channel events
|
|
136
|
-
*
|
|
137
|
-
* Use the ref returned from a channel.on() to unsubscribe one
|
|
138
|
-
* handler, or pass nothing for the ref to unsubscribe all
|
|
139
|
-
* handlers for the given event.
|
|
140
|
-
*
|
|
141
|
-
* @example
|
|
142
|
-
* // Unsubscribe the do_stuff handler
|
|
143
|
-
* const ref1 = channel.on("event", do_stuff)
|
|
144
|
-
* channel.off("event", ref1)
|
|
145
|
-
*
|
|
146
|
-
* // Unsubscribe all handlers from event
|
|
147
|
-
* channel.off("event")
|
|
148
|
-
*
|
|
149
|
-
* @param {string} event
|
|
150
|
-
* @param {integer} ref
|
|
151
|
-
*/
|
|
152
|
-
off(event, ref) {
|
|
153
|
-
this.bindings = this.bindings.filter((bind) => {
|
|
154
|
-
return !(bind.event === event && (typeof ref === "undefined" || ref === bind.ref));
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* @private
|
|
159
|
-
*/
|
|
160
|
-
canPush() { return this.socket.isConnected() && this.isJoined(); }
|
|
161
|
-
/**
|
|
162
|
-
* Sends a message `event` to phoenix with the payload `payload`.
|
|
163
|
-
* Phoenix receives this in the `handle_in(event, payload, socket)`
|
|
164
|
-
* function. if phoenix replies or it times out (default 10000ms),
|
|
165
|
-
* then optionally the reply can be received.
|
|
166
|
-
*
|
|
167
|
-
* @example
|
|
168
|
-
* channel.push("event")
|
|
169
|
-
* .receive("ok", payload => console.log("phoenix replied:", payload))
|
|
170
|
-
* .receive("error", err => console.log("phoenix errored", err))
|
|
171
|
-
* .receive("timeout", () => console.log("timed out pushing"))
|
|
172
|
-
* @param {string} event
|
|
173
|
-
* @param {Object} payload
|
|
174
|
-
* @param {number} [timeout]
|
|
175
|
-
* @returns {Push}
|
|
176
|
-
*/
|
|
177
|
-
push(event, payload, timeout = this.timeout) {
|
|
178
|
-
payload = payload || {};
|
|
179
|
-
if (!this.joinedOnce) {
|
|
180
|
-
throw new Error(`tried to push '${event}' to '${this.topic}' before joining. Use channel.join() before pushing events`);
|
|
181
|
-
}
|
|
182
|
-
let pushEvent = new Push(this, event, function () { return payload; }, timeout);
|
|
183
|
-
if (this.canPush()) {
|
|
184
|
-
pushEvent.send();
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
pushEvent.startTimeout();
|
|
188
|
-
this.pushBuffer.push(pushEvent);
|
|
189
|
-
}
|
|
190
|
-
return pushEvent;
|
|
191
|
-
}
|
|
192
|
-
/** Leaves the channel
|
|
193
|
-
*
|
|
194
|
-
* Unsubscribes from server events, and
|
|
195
|
-
* instructs channel to terminate on server
|
|
196
|
-
*
|
|
197
|
-
* Triggers onClose() hooks
|
|
198
|
-
*
|
|
199
|
-
* To receive leave acknowledgements, use the `receive`
|
|
200
|
-
* hook to bind to the server ack, ie:
|
|
201
|
-
*
|
|
202
|
-
* @example
|
|
203
|
-
* channel.leave().receive("ok", () => alert("left!") )
|
|
204
|
-
*
|
|
205
|
-
* @param {integer} timeout
|
|
206
|
-
* @returns {Push}
|
|
207
|
-
*/
|
|
208
|
-
leave(timeout = this.timeout) {
|
|
209
|
-
this.rejoinTimer.reset();
|
|
210
|
-
this.joinPush.cancelTimeout();
|
|
211
|
-
this.state = CHANNEL_STATES.leaving;
|
|
212
|
-
let onClose = () => {
|
|
213
|
-
if (this.socket.hasLogger())
|
|
214
|
-
this.socket.log("channel", `leave ${this.topic}`);
|
|
215
|
-
this.trigger(CHANNEL_EVENTS.close, "leave");
|
|
216
|
-
};
|
|
217
|
-
let leavePush = new Push(this, CHANNEL_EVENTS.leave, closure({}), timeout);
|
|
218
|
-
leavePush.receive("ok", () => onClose())
|
|
219
|
-
.receive("timeout", () => onClose());
|
|
220
|
-
leavePush.send();
|
|
221
|
-
if (!this.canPush()) {
|
|
222
|
-
leavePush.trigger("ok", {});
|
|
223
|
-
}
|
|
224
|
-
return leavePush;
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* Overridable message hook
|
|
228
|
-
*
|
|
229
|
-
* Receives all events for specialized message handling
|
|
230
|
-
* before dispatching to the channel callbacks.
|
|
231
|
-
*
|
|
232
|
-
* Must return the payload, modified or unmodified
|
|
233
|
-
* @param {string} event
|
|
234
|
-
* @param {Object} payload
|
|
235
|
-
* @param {integer} ref
|
|
236
|
-
* @returns {Object}
|
|
237
|
-
*/
|
|
238
|
-
onMessage(_event, payload, _ref) { return payload; }
|
|
239
|
-
/**
|
|
240
|
-
* @private
|
|
241
|
-
*/
|
|
242
|
-
isMember(topic, event, payload, joinRef) {
|
|
243
|
-
if (this.topic !== topic) {
|
|
244
|
-
return false;
|
|
245
|
-
}
|
|
246
|
-
if (joinRef && joinRef !== this.joinRef()) {
|
|
247
|
-
if (this.socket.hasLogger())
|
|
248
|
-
this.socket.log("channel", "dropping outdated message", { topic, event, payload, joinRef });
|
|
249
|
-
return false;
|
|
250
|
-
}
|
|
251
|
-
else {
|
|
252
|
-
return true;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* @private
|
|
257
|
-
*/
|
|
258
|
-
joinRef() { return this.joinPush.ref; }
|
|
259
|
-
/**
|
|
260
|
-
* @private
|
|
261
|
-
*/
|
|
262
|
-
rejoin(timeout = this.timeout) {
|
|
263
|
-
if (this.isLeaving()) {
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
this.socket.leaveOpenTopic(this.topic);
|
|
267
|
-
this.state = CHANNEL_STATES.joining;
|
|
268
|
-
this.joinPush.resend(timeout);
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* @private
|
|
272
|
-
*/
|
|
273
|
-
trigger(event, payload, ref, joinRef) {
|
|
274
|
-
let handledPayload = this.onMessage(event, payload, ref, joinRef);
|
|
275
|
-
if (payload && !handledPayload) {
|
|
276
|
-
throw new Error("channel onMessage callbacks must return the payload, modified or unmodified");
|
|
277
|
-
}
|
|
278
|
-
let eventBindings = this.bindings.filter(bind => bind.event === event);
|
|
279
|
-
for (let i = 0; i < eventBindings.length; i++) {
|
|
280
|
-
let bind = eventBindings[i];
|
|
281
|
-
bind.callback(handledPayload, ref, joinRef || this.joinRef());
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
* @private
|
|
286
|
-
*/
|
|
287
|
-
replyEventName(ref) { return `chan_reply_${ref}`; }
|
|
288
|
-
/**
|
|
289
|
-
* @private
|
|
290
|
-
*/
|
|
291
|
-
isClosed() { return this.state === CHANNEL_STATES.closed; }
|
|
292
|
-
/**
|
|
293
|
-
* @private
|
|
294
|
-
*/
|
|
295
|
-
isErrored() { return this.state === CHANNEL_STATES.errored; }
|
|
296
|
-
/**
|
|
297
|
-
* @private
|
|
298
|
-
*/
|
|
299
|
-
isJoined() { return this.state === CHANNEL_STATES.joined; }
|
|
300
|
-
/**
|
|
301
|
-
* @private
|
|
302
|
-
*/
|
|
303
|
-
isJoining() { return this.state === CHANNEL_STATES.joining; }
|
|
304
|
-
/**
|
|
305
|
-
* @private
|
|
306
|
-
*/
|
|
307
|
-
isLeaving() { return this.state === CHANNEL_STATES.leaving; }
|
|
308
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
export declare const globalSelf: (Window & typeof globalThis) | null;
|
|
2
|
-
export declare const phxWindow: (Window & typeof globalThis) | null;
|
|
3
|
-
export declare const global: {};
|
|
4
|
-
export declare const DEFAULT_VSN = "2.0.0";
|
|
5
|
-
export declare const SOCKET_STATES: {
|
|
6
|
-
connecting: number;
|
|
7
|
-
open: number;
|
|
8
|
-
closing: number;
|
|
9
|
-
closed: number;
|
|
10
|
-
};
|
|
11
|
-
export declare const DEFAULT_TIMEOUT = 10000;
|
|
12
|
-
export declare const WS_CLOSE_NORMAL = 1000;
|
|
13
|
-
export declare const CHANNEL_STATES: {
|
|
14
|
-
closed: string;
|
|
15
|
-
errored: string;
|
|
16
|
-
joined: string;
|
|
17
|
-
joining: string;
|
|
18
|
-
leaving: string;
|
|
19
|
-
};
|
|
20
|
-
export declare const CHANNEL_EVENTS: {
|
|
21
|
-
close: string;
|
|
22
|
-
error: string;
|
|
23
|
-
join: string;
|
|
24
|
-
reply: string;
|
|
25
|
-
leave: string;
|
|
26
|
-
};
|
|
27
|
-
export declare const TRANSPORTS: {
|
|
28
|
-
longpoll: string;
|
|
29
|
-
websocket: string;
|
|
30
|
-
};
|
|
31
|
-
export declare const XHR_STATES: {
|
|
32
|
-
complete: number;
|
|
33
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
export const globalSelf = typeof self !== "undefined" ? self : null;
|
|
3
|
-
export const phxWindow = typeof window !== "undefined" ? window : null;
|
|
4
|
-
export const global = globalSelf || phxWindow || {};
|
|
5
|
-
export const DEFAULT_VSN = "2.0.0";
|
|
6
|
-
export const SOCKET_STATES = { connecting: 0, open: 1, closing: 2, closed: 3 };
|
|
7
|
-
export const DEFAULT_TIMEOUT = 10000;
|
|
8
|
-
export const WS_CLOSE_NORMAL = 1000;
|
|
9
|
-
export const CHANNEL_STATES = {
|
|
10
|
-
closed: "closed",
|
|
11
|
-
errored: "errored",
|
|
12
|
-
joined: "joined",
|
|
13
|
-
joining: "joining",
|
|
14
|
-
leaving: "leaving",
|
|
15
|
-
};
|
|
16
|
-
export const CHANNEL_EVENTS = {
|
|
17
|
-
close: "phx_close",
|
|
18
|
-
error: "phx_error",
|
|
19
|
-
join: "phx_join",
|
|
20
|
-
reply: "phx_reply",
|
|
21
|
-
leave: "phx_leave"
|
|
22
|
-
};
|
|
23
|
-
export const TRANSPORTS = {
|
|
24
|
-
longpoll: "longpoll",
|
|
25
|
-
websocket: "websocket"
|
|
26
|
-
};
|
|
27
|
-
export const XHR_STATES = {
|
|
28
|
-
complete: 4
|
|
29
|
-
};
|
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Phoenix Channels JavaScript client
|
|
3
|
-
*
|
|
4
|
-
* ## Socket Connection
|
|
5
|
-
*
|
|
6
|
-
* A single connection is established to the server and
|
|
7
|
-
* channels are multiplexed over the connection.
|
|
8
|
-
* Connect to the server using the `Socket` class:
|
|
9
|
-
*
|
|
10
|
-
* ```javascript
|
|
11
|
-
* let socket = new Socket("/socket", {params: {userToken: "123"}})
|
|
12
|
-
* socket.connect()
|
|
13
|
-
* ```
|
|
14
|
-
*
|
|
15
|
-
* The `Socket` constructor takes the mount point of the socket,
|
|
16
|
-
* the authentication params, as well as options that can be found in
|
|
17
|
-
* the Socket docs, such as configuring the `LongPoll` transport, and
|
|
18
|
-
* heartbeat.
|
|
19
|
-
*
|
|
20
|
-
* ## Channels
|
|
21
|
-
*
|
|
22
|
-
* Channels are isolated, concurrent processes on the server that
|
|
23
|
-
* subscribe to topics and broker events between the client and server.
|
|
24
|
-
* To join a channel, you must provide the topic, and channel params for
|
|
25
|
-
* authorization. Here's an example chat room example where `"new_msg"`
|
|
26
|
-
* events are listened for, messages are pushed to the server, and
|
|
27
|
-
* the channel is joined with ok/error/timeout matches:
|
|
28
|
-
*
|
|
29
|
-
* ```javascript
|
|
30
|
-
* let channel = socket.channel("room:123", {token: roomToken})
|
|
31
|
-
* channel.on("new_msg", msg => console.log("Got message", msg) )
|
|
32
|
-
* $input.onEnter( e => {
|
|
33
|
-
* channel.push("new_msg", {body: e.target.val}, 10000)
|
|
34
|
-
* .receive("ok", (msg) => console.log("created message", msg) )
|
|
35
|
-
* .receive("error", (reasons) => console.log("create failed", reasons) )
|
|
36
|
-
* .receive("timeout", () => console.log("Networking issue...") )
|
|
37
|
-
* })
|
|
38
|
-
*
|
|
39
|
-
* channel.join()
|
|
40
|
-
* .receive("ok", ({messages}) => console.log("catching up", messages) )
|
|
41
|
-
* .receive("error", ({reason}) => console.log("failed join", reason) )
|
|
42
|
-
* .receive("timeout", () => console.log("Networking issue. Still waiting..."))
|
|
43
|
-
*```
|
|
44
|
-
*
|
|
45
|
-
* ## Joining
|
|
46
|
-
*
|
|
47
|
-
* Creating a channel with `socket.channel(topic, params)`, binds the params to
|
|
48
|
-
* `channel.params`, which are sent up on `channel.join()`.
|
|
49
|
-
* Subsequent rejoins will send up the modified params for
|
|
50
|
-
* updating authorization params, or passing up last_message_id information.
|
|
51
|
-
* Successful joins receive an "ok" status, while unsuccessful joins
|
|
52
|
-
* receive "error".
|
|
53
|
-
*
|
|
54
|
-
* With the default serializers and WebSocket transport, JSON text frames are
|
|
55
|
-
* used for pushing a JSON object literal. If an `ArrayBuffer` instance is provided,
|
|
56
|
-
* binary encoding will be used and the message will be sent with the binary
|
|
57
|
-
* opcode.
|
|
58
|
-
*
|
|
59
|
-
* *Note*: binary messages are only supported on the WebSocket transport.
|
|
60
|
-
*
|
|
61
|
-
* ## Duplicate Join Subscriptions
|
|
62
|
-
*
|
|
63
|
-
* While the client may join any number of topics on any number of channels,
|
|
64
|
-
* the client may only hold a single subscription for each unique topic at any
|
|
65
|
-
* given time. When attempting to create a duplicate subscription,
|
|
66
|
-
* the server will close the existing channel, log a warning, and
|
|
67
|
-
* spawn a new channel for the topic. The client will have their
|
|
68
|
-
* `channel.onClose` callbacks fired for the existing channel, and the new
|
|
69
|
-
* channel join will have its receive hooks processed as normal.
|
|
70
|
-
*
|
|
71
|
-
* ## Pushing Messages
|
|
72
|
-
*
|
|
73
|
-
* From the previous example, we can see that pushing messages to the server
|
|
74
|
-
* can be done with `channel.push(eventName, payload)` and we can optionally
|
|
75
|
-
* receive responses from the push. Additionally, we can use
|
|
76
|
-
* `receive("timeout", callback)` to abort waiting for our other `receive` hooks
|
|
77
|
-
* and take action after some period of waiting. The default timeout is 10000ms.
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* ## Socket Hooks
|
|
81
|
-
*
|
|
82
|
-
* Lifecycle events of the multiplexed connection can be hooked into via
|
|
83
|
-
* `socket.onError()` and `socket.onClose()` events, ie:
|
|
84
|
-
*
|
|
85
|
-
* ```javascript
|
|
86
|
-
* socket.onError( () => console.log("there was an error with the connection!") )
|
|
87
|
-
* socket.onClose( () => console.log("the connection dropped") )
|
|
88
|
-
* ```
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* ## Channel Hooks
|
|
92
|
-
*
|
|
93
|
-
* For each joined channel, you can bind to `onError` and `onClose` events
|
|
94
|
-
* to monitor the channel lifecycle, ie:
|
|
95
|
-
*
|
|
96
|
-
* ```javascript
|
|
97
|
-
* channel.onError( () => console.log("there was an error!") )
|
|
98
|
-
* channel.onClose( () => console.log("the channel has gone away gracefully") )
|
|
99
|
-
* ```
|
|
100
|
-
*
|
|
101
|
-
* ### onError hooks
|
|
102
|
-
*
|
|
103
|
-
* `onError` hooks are invoked if the socket connection drops, or the channel
|
|
104
|
-
* crashes on the server. In either case, a channel rejoin is attempted
|
|
105
|
-
* automatically in an exponential backoff manner.
|
|
106
|
-
*
|
|
107
|
-
* ### onClose hooks
|
|
108
|
-
*
|
|
109
|
-
* `onClose` hooks are invoked only in two cases. 1) the channel explicitly
|
|
110
|
-
* closed on the server, or 2). The client explicitly closed, by calling
|
|
111
|
-
* `channel.leave()`
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
* ## Presence
|
|
115
|
-
*
|
|
116
|
-
* The `Presence` object provides features for syncing presence information
|
|
117
|
-
* from the server with the client and handling presences joining and leaving.
|
|
118
|
-
*
|
|
119
|
-
* ### Syncing state from the server
|
|
120
|
-
*
|
|
121
|
-
* To sync presence state from the server, first instantiate an object and
|
|
122
|
-
* pass your channel in to track lifecycle events:
|
|
123
|
-
*
|
|
124
|
-
* ```javascript
|
|
125
|
-
* let channel = socket.channel("some:topic")
|
|
126
|
-
* let presence = new Presence(channel)
|
|
127
|
-
* ```
|
|
128
|
-
*
|
|
129
|
-
* Next, use the `presence.onSync` callback to react to state changes
|
|
130
|
-
* from the server. For example, to render the list of users every time
|
|
131
|
-
* the list changes, you could write:
|
|
132
|
-
*
|
|
133
|
-
* ```javascript
|
|
134
|
-
* presence.onSync(() => {
|
|
135
|
-
* myRenderUsersFunction(presence.list())
|
|
136
|
-
* })
|
|
137
|
-
* ```
|
|
138
|
-
*
|
|
139
|
-
* ### Listing Presences
|
|
140
|
-
*
|
|
141
|
-
* `presence.list` is used to return a list of presence information
|
|
142
|
-
* based on the local state of metadata. By default, all presence
|
|
143
|
-
* metadata is returned, but a `listBy` function can be supplied to
|
|
144
|
-
* allow the client to select which metadata to use for a given presence.
|
|
145
|
-
* For example, you may have a user online from different devices with
|
|
146
|
-
* a metadata status of "online", but they have set themselves to "away"
|
|
147
|
-
* on another device. In this case, the app may choose to use the "away"
|
|
148
|
-
* status for what appears on the UI. The example below defines a `listBy`
|
|
149
|
-
* function which prioritizes the first metadata which was registered for
|
|
150
|
-
* each user. This could be the first tab they opened, or the first device
|
|
151
|
-
* they came online from:
|
|
152
|
-
*
|
|
153
|
-
* ```javascript
|
|
154
|
-
* let listBy = (id, {metas: [first, ...rest]}) => {
|
|
155
|
-
* first.count = rest.length + 1 // count of this user's presences
|
|
156
|
-
* first.id = id
|
|
157
|
-
* return first
|
|
158
|
-
* }
|
|
159
|
-
* let onlineUsers = presence.list(listBy)
|
|
160
|
-
* ```
|
|
161
|
-
*
|
|
162
|
-
* ### Handling individual presence join and leave events
|
|
163
|
-
*
|
|
164
|
-
* The `presence.onJoin` and `presence.onLeave` callbacks can be used to
|
|
165
|
-
* react to individual presences joining and leaving the app. For example:
|
|
166
|
-
*
|
|
167
|
-
* ```javascript
|
|
168
|
-
* let presence = new Presence(channel)
|
|
169
|
-
*
|
|
170
|
-
* // detect if user has joined for the 1st time or from another tab/device
|
|
171
|
-
* presence.onJoin((id, current, newPres) => {
|
|
172
|
-
* if(!current){
|
|
173
|
-
* console.log("user has entered for the first time", newPres)
|
|
174
|
-
* } else {
|
|
175
|
-
* console.log("user additional presence", newPres)
|
|
176
|
-
* }
|
|
177
|
-
* })
|
|
178
|
-
*
|
|
179
|
-
* // detect if user has left from all tabs/devices, or is still present
|
|
180
|
-
* presence.onLeave((id, current, leftPres) => {
|
|
181
|
-
* if(current.metas.length === 0){
|
|
182
|
-
* console.log("user has left from all devices", leftPres)
|
|
183
|
-
* } else {
|
|
184
|
-
* console.log("user left from a device", leftPres)
|
|
185
|
-
* }
|
|
186
|
-
* })
|
|
187
|
-
* // receive presence data from server
|
|
188
|
-
* presence.onSync(() => {
|
|
189
|
-
* displayUsers(presence.list())
|
|
190
|
-
* })
|
|
191
|
-
* ```
|
|
192
|
-
* @module phoenix
|
|
193
|
-
*/
|
|
194
|
-
import Channel from "./channel.js";
|
|
195
|
-
import LongPoll from "./longpoll.js";
|
|
196
|
-
import Presence from "./presence.js";
|
|
197
|
-
import Serializer from "./serializer.js";
|
|
198
|
-
import Socket from "./socket.js";
|
|
199
|
-
export { Channel, LongPoll, Presence, Serializer, Socket };
|