@typetron/websockets 0.3.13 → 0.4.0-rc7
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/index.d.ts +22 -22
- package/index.d.ts.map +1 -1
- package/index.js +73 -78
- package/index.js.map +1 -1
- package/package.json +43 -43
- package/Readme.md +0 -86
package/index.d.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { Observable, ReplaySubject, Subject } from 'rxjs';
|
|
2
|
-
import {
|
|
3
|
-
export declare class Websocket {
|
|
4
|
-
url: string;
|
|
5
|
-
protocols?: string | string[] | undefined;
|
|
6
|
-
socket?: WebSocket;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
errors$: Subject<
|
|
10
|
-
onConnectCallback:
|
|
11
|
-
constructor(url: string, protocols?: string | string[] | undefined);
|
|
12
|
-
get isConnected(): boolean;
|
|
13
|
-
connect(url?: string, protocols?: string | string[]): void;
|
|
14
|
-
onConnect(callback:
|
|
15
|
-
reconnect(): void;
|
|
16
|
-
emit<T>(
|
|
17
|
-
on<T>(
|
|
18
|
-
onError({ except }: {
|
|
19
|
-
except?: string[];
|
|
20
|
-
}): Observable<
|
|
21
|
-
request<T>(
|
|
22
|
-
}
|
|
1
|
+
import { Observable, ReplaySubject, Subject } from 'rxjs';
|
|
2
|
+
import { ActionErrorResponse, ActionRequest, ActionResponse } from '@typetron/framework/Router/Websockets/types';
|
|
3
|
+
export declare class Websocket {
|
|
4
|
+
url: string;
|
|
5
|
+
protocols?: string | string[] | undefined;
|
|
6
|
+
socket?: WebSocket;
|
|
7
|
+
actionMessages$: Subject<ActionResponse<unknown>>;
|
|
8
|
+
queuedActions$: ReplaySubject<ActionRequest>;
|
|
9
|
+
errors$: Subject<ActionErrorResponse>;
|
|
10
|
+
onConnectCallback: () => void;
|
|
11
|
+
constructor(url: string, protocols?: string | string[] | undefined);
|
|
12
|
+
get isConnected(): boolean;
|
|
13
|
+
connect(url?: string, protocols?: string | string[]): void;
|
|
14
|
+
onConnect(callback: () => void): void;
|
|
15
|
+
reconnect(): void;
|
|
16
|
+
emit<T>(action: string, request?: ActionRequest['message']): void;
|
|
17
|
+
on<T>(action: string): Observable<T>;
|
|
18
|
+
onError({ except }: {
|
|
19
|
+
except?: string[];
|
|
20
|
+
}): Observable<ActionResponse<unknown>>;
|
|
21
|
+
request<T>(action: string, message?: ActionRequest['message']): Promise<T>;
|
|
22
|
+
}
|
|
23
23
|
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAM,aAAa,EAAE,OAAO,EAAc,MAAM,MAAM,CAAA;AAEzE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAM,aAAa,EAAE,OAAO,EAAc,MAAM,MAAM,CAAA;AAEzE,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,cAAc,EAA0B,MAAM,6CAA6C,CAAA;AAExI,qBAAa,SAAS;IAQC,GAAG,EAAE,MAAM;IAAS,SAAS,CAAC;IAPjD,MAAM,CAAC,EAAE,SAAS,CAAA;IAElB,eAAe,mCAAyC;IACxD,cAAc,+BAAqC;IACnD,OAAO,+BAAqC;IAC5C,iBAAiB,EAAE,MAAM,IAAI,CAAA;gBAEV,GAAG,EAAE,MAAM,EAAS,SAAS,CAAC,+BAAmB;IAIpE,IAAI,WAAW,YAEd;IAED,OAAO,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI;IAqB1D,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI;IAI9B,SAAS,IAAI,IAAI;IAQjB,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,CAAC,SAAS,CAAC,GAAG,IAAI;IAKjE,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,CAAC,CAAC;IAcpC,OAAO,CAAC,EAAC,MAAM,EAAC,EAAE;QAAC,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;KAAC,GAAG,UAAU,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAIrE,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CASnF"}
|
package/index.js
CHANGED
|
@@ -1,79 +1,74 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Websocket = void 0;
|
|
4
|
-
const rxjs_1 = require("rxjs");
|
|
5
|
-
const operators_1 = require("rxjs/operators");
|
|
6
|
-
const types_1 = require("@typetron/framework/Router/Websockets/types");
|
|
7
|
-
class Websocket {
|
|
8
|
-
constructor(url, protocols) {
|
|
9
|
-
this.url = url;
|
|
10
|
-
this.protocols = protocols;
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
13
|
-
|
|
14
|
-
this.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
this.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
exports.Websocket = Websocket;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Websocket = void 0;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
const operators_1 = require("rxjs/operators");
|
|
6
|
+
const types_1 = require("@typetron/framework/Router/Websockets/types");
|
|
7
|
+
class Websocket {
|
|
8
|
+
constructor(url, protocols) {
|
|
9
|
+
this.url = url;
|
|
10
|
+
this.protocols = protocols;
|
|
11
|
+
this.actionMessages$ = new rxjs_1.Subject();
|
|
12
|
+
this.queuedActions$ = new rxjs_1.ReplaySubject();
|
|
13
|
+
this.errors$ = new rxjs_1.Subject();
|
|
14
|
+
this.connect(url, protocols);
|
|
15
|
+
}
|
|
16
|
+
get isConnected() {
|
|
17
|
+
return this.socket?.readyState === WebSocket.OPEN;
|
|
18
|
+
}
|
|
19
|
+
connect(url, protocols) {
|
|
20
|
+
const socket = new WebSocket(url ?? this.url, protocols ?? this.protocols);
|
|
21
|
+
this.socket = socket;
|
|
22
|
+
socket.onmessage = (action) => {
|
|
23
|
+
const message = JSON.parse(action.data);
|
|
24
|
+
this.actionMessages$.next(message);
|
|
25
|
+
};
|
|
26
|
+
socket.onopen = () => {
|
|
27
|
+
this.onConnectCallback?.();
|
|
28
|
+
this.queuedActions$.subscribe(message => {
|
|
29
|
+
socket.send(JSON.stringify(message));
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
socket.onclose = (action) => {
|
|
33
|
+
this.queuedActions$ = new rxjs_1.ReplaySubject();
|
|
34
|
+
this.reconnect();
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
onConnect(callback) {
|
|
38
|
+
this.onConnectCallback = callback;
|
|
39
|
+
}
|
|
40
|
+
reconnect() {
|
|
41
|
+
if (this.socket?.readyState === WebSocket.CLOSED) {
|
|
42
|
+
window.setTimeout(() => {
|
|
43
|
+
this.connect(this.url, this.protocols);
|
|
44
|
+
}, 1000);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
emit(action, request) {
|
|
48
|
+
const actionMessage = { action, message: request };
|
|
49
|
+
this.queuedActions$.next(actionMessage);
|
|
50
|
+
}
|
|
51
|
+
on(action) {
|
|
52
|
+
return this.actionMessages$.pipe((0, operators_1.filter)(actionMessage => actionMessage.action === action), (0, operators_1.switchMap)(actionMessage => {
|
|
53
|
+
if (actionMessage.status === types_1.WebsocketMessageStatus.Error) {
|
|
54
|
+
this.errors$.next(actionMessage);
|
|
55
|
+
return (0, rxjs_1.throwError)(actionMessage);
|
|
56
|
+
}
|
|
57
|
+
return (0, rxjs_1.of)(actionMessage);
|
|
58
|
+
}), (0, operators_1.map)(actionMessage => actionMessage.message));
|
|
59
|
+
}
|
|
60
|
+
onError({ except }) {
|
|
61
|
+
return this.errors$.pipe((0, operators_1.filter)(actionMessage => !(except || []).includes(actionMessage.action)));
|
|
62
|
+
}
|
|
63
|
+
async request(action, message) {
|
|
64
|
+
this.emit(action, message);
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
this.on(action).pipe((0, operators_1.take)(1)).subscribe({
|
|
67
|
+
next: resolve,
|
|
68
|
+
error: reject
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.Websocket = Websocket;
|
|
79
74
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,+BAAyE;AACzE,8CAA6D;AAC7D,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,+BAAyE;AACzE,8CAA6D;AAC7D,uEAAwI;AAExI,MAAa,SAAS;IAQlB,YAAmB,GAAW,EAAS,SAA6B;QAAjD,QAAG,GAAH,GAAG,CAAQ;QAAS,cAAS,GAAT,SAAS,CAAoB;QALpE,oBAAe,GAAG,IAAI,cAAO,EAA2B,CAAA;QACxD,mBAAc,GAAG,IAAI,oBAAa,EAAiB,CAAA;QACnD,YAAO,GAAG,IAAI,cAAO,EAAuB,CAAA;QAIxC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,SAAS,CAAC,CAAA;IAChC,CAAC;IAED,IAAI,WAAW;QACX,OAAO,IAAI,CAAC,MAAM,EAAE,UAAU,KAAK,SAAS,CAAC,IAAI,CAAA;IACrD,CAAC;IAED,OAAO,CAAC,GAAY,EAAE,SAA6B;QAC/C,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,EAAE,SAAS,IAAI,IAAI,CAAC,SAAS,CAAC,CAAA;QAC1E,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QAEpB,MAAM,CAAC,SAAS,GAAG,CAAC,MAAM,EAAE,EAAE;YAC1B,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;YACvC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QACtC,CAAC,CAAA;QACD,MAAM,CAAC,MAAM,GAAG,GAAG,EAAE;YACjB,IAAI,CAAC,iBAAiB,EAAE,EAAE,CAAA;YAC1B,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE;gBACpC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAA;YACxC,CAAC,CAAC,CAAA;QACN,CAAC,CAAA;QAED,MAAM,CAAC,OAAO,GAAG,CAAC,MAAM,EAAE,EAAE;YACxB,IAAI,CAAC,cAAc,GAAG,IAAI,oBAAa,EAAiB,CAAA;YACxD,IAAI,CAAC,SAAS,EAAE,CAAA;QACpB,CAAC,CAAA;IACL,CAAC;IAED,SAAS,CAAC,QAAoB;QAC1B,IAAI,CAAC,iBAAiB,GAAG,QAAQ,CAAA;IACrC,CAAC;IAED,SAAS;QACL,IAAI,IAAI,CAAC,MAAM,EAAE,UAAU,KAAK,SAAS,CAAC,MAAM,EAAE;YAC9C,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE;gBACnB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;YAC1C,CAAC,EAAE,IAAI,CAAC,CAAA;SACX;IACL,CAAC;IAED,IAAI,CAAI,MAAc,EAAE,OAAkC;QACtD,MAAM,aAAa,GAAkB,EAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAC,CAAA;QAC/D,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;IAC3C,CAAC;IAED,EAAE,CAAI,MAAc;QAChB,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAC5B,IAAA,kBAAM,EAAC,aAAa,CAAC,EAAE,CAAC,aAAa,CAAC,MAAM,KAAK,MAAM,CAAC,EACxD,IAAA,qBAAS,EAAC,aAAa,CAAC,EAAE;YACtB,IAAI,aAAa,CAAC,MAAM,KAAK,8BAAsB,CAAC,KAAK,EAAE;gBACvD,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,aAAoC,CAAC,CAAA;gBACvD,OAAO,IAAA,iBAAU,EAAC,aAAa,CAAC,CAAA;aACnC;YACD,OAAO,IAAA,SAAE,EAAC,aAAa,CAAC,CAAA;QAC5B,CAAC,CAAC,EACF,IAAA,eAAG,EAAC,aAAa,CAAC,EAAE,CAAC,aAAa,CAAC,OAAO,CAAC,CAC7B,CAAA;IACtB,CAAC;IAED,OAAO,CAAC,EAAC,MAAM,EAAsB;QACjC,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAA,kBAAM,EAAC,aAAa,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;IACrG,CAAC;IAED,KAAK,CAAC,OAAO,CAAI,MAAc,EAAE,OAAkC;QAC/D,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;QAC1B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACnC,IAAI,CAAC,EAAE,CAAI,MAAM,CAAC,CAAC,IAAI,CAAC,IAAA,gBAAI,EAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;gBACvC,IAAI,EAAE,OAAO;gBACb,KAAK,EAAE,MAAM;aAChB,CAAC,CAAA;QACN,CAAC,CAAC,CAAA;IACN,CAAC;CACJ;AAjFD,8BAiFC"}
|
package/package.json
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@typetron/websockets",
|
|
3
|
-
"description": "Typetron Websockets library",
|
|
4
|
-
"keywords": [
|
|
5
|
-
"websockets",
|
|
6
|
-
"typetron",
|
|
7
|
-
"nodejs",
|
|
8
|
-
" typescript",
|
|
9
|
-
"real-time",
|
|
10
|
-
"realtime"
|
|
11
|
-
],
|
|
12
|
-
"license": "MIT",
|
|
13
|
-
"version": "0.
|
|
14
|
-
"author": {
|
|
15
|
-
"email": "ionel@typetron.org",
|
|
16
|
-
"name": "Ionel Lupu"
|
|
17
|
-
},
|
|
18
|
-
"scripts": {
|
|
19
|
-
"build": "tsc"
|
|
20
|
-
},
|
|
21
|
-
"engines": {
|
|
22
|
-
"node": ">= 12.13.0"
|
|
23
|
-
},
|
|
24
|
-
"peerDependencies": {
|
|
25
|
-
"rxjs": "~7.
|
|
26
|
-
},
|
|
27
|
-
"dependencies": {
|
|
28
|
-
"@typetron/framework": "
|
|
29
|
-
},
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"typescript": "
|
|
32
|
-
},
|
|
33
|
-
"repository": {
|
|
34
|
-
"type": "git",
|
|
35
|
-
"url": "https://github.com/typetron/websockets.git"
|
|
36
|
-
},
|
|
37
|
-
"files": [
|
|
38
|
-
"**/*.ts",
|
|
39
|
-
"**/*.js",
|
|
40
|
-
"**/*.ts.map",
|
|
41
|
-
"**/*.js.map"
|
|
42
|
-
]
|
|
43
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@typetron/websockets",
|
|
3
|
+
"description": "Typetron Websockets library",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"websockets",
|
|
6
|
+
"typetron",
|
|
7
|
+
"nodejs",
|
|
8
|
+
" typescript",
|
|
9
|
+
"real-time",
|
|
10
|
+
"realtime"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"version": "0.4.0-rc7",
|
|
14
|
+
"author": {
|
|
15
|
+
"email": "ionel@typetron.org",
|
|
16
|
+
"name": "Ionel Lupu"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc --watch"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">= 12.13.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"rxjs": "~7.8.1"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@typetron/framework": "0.4.0-rc7"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"typescript": "5.2.2"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/typetron/websockets.git"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"**/*.ts",
|
|
39
|
+
"**/*.js",
|
|
40
|
+
"**/*.ts.map",
|
|
41
|
+
"**/*.js.map"
|
|
42
|
+
]
|
|
43
|
+
}
|
package/Readme.md
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
# Typetron WebSockets
|
|
2
|
-
|
|
3
|
-
This library is used to easily listen and emit WebSockets event from the browser client to a Typetron app.
|
|
4
|
-
|
|
5
|
-
#### Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
$ npm install @typetron/websockets
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
Note: you need to have a module loader in your app for this to work (webpack, rollup, babel, parcel, etc.)
|
|
12
|
-
|
|
13
|
-
#### Listening for events
|
|
14
|
-
|
|
15
|
-
You can listen to a WebSocket event sent from the server using the _on('Event name')_ method like this on the socket
|
|
16
|
-
connection:
|
|
17
|
-
|
|
18
|
-
```ts
|
|
19
|
-
socket.on('eventName').subscribe(response => {
|
|
20
|
-
console.log('Backend response', response)
|
|
21
|
-
})
|
|
22
|
-
socket.on<User>('user.update').subscribe(user => {
|
|
23
|
-
console.log('User updated', user)
|
|
24
|
-
})
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
The _on_ method will return an observable (see [RxJS](https://rxjs.dev/) for more details) that you can use to subscribe
|
|
28
|
-
to.
|
|
29
|
-
|
|
30
|
-
#### Emitting events
|
|
31
|
-
|
|
32
|
-
If you want to signal the server with and event or when you want to send some data to it, you can use the _emit('event
|
|
33
|
-
name', data)_ method:
|
|
34
|
-
|
|
35
|
-
```ts
|
|
36
|
-
socket.emit('eventName');
|
|
37
|
-
socket.emit('eventName', "my message here");
|
|
38
|
-
socket.emit('eventName', {my: {message: "here"}});
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
Be aware that if you are expecting a response from the backend, you need to subscribe to the same event (or the event
|
|
42
|
-
the server is emitting to) using the _.on_ method.
|
|
43
|
-
|
|
44
|
-
#### Emitting and listening for server response
|
|
45
|
-
|
|
46
|
-
If you want to make a single "request" to the server, meaning that you want to emit and wait for a response at the same
|
|
47
|
-
time, you can use the _request('event name', data?)_ method. This will essentially make an _emit_ and listen to its
|
|
48
|
-
response using the _on_ method for you:
|
|
49
|
-
|
|
50
|
-
```ts
|
|
51
|
-
const users = await socket.request<User[]>('users.list')
|
|
52
|
-
const savedUser = await socket.request<User>('users.save', {name: 'John'})
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
#### Message format of the WebSocket events
|
|
56
|
-
|
|
57
|
-
The Typetron WebSocket server uses a specific message format when exchanging information between it and the clients.
|
|
58
|
-
These message have the following format:
|
|
59
|
-
_When sending a message:_
|
|
60
|
-
|
|
61
|
-
```json
|
|
62
|
-
{
|
|
63
|
-
"event": "event name",
|
|
64
|
-
"message": {
|
|
65
|
-
// optional
|
|
66
|
-
"body": "content sent to the controllers",
|
|
67
|
-
"parameters": [
|
|
68
|
-
"param1",
|
|
69
|
-
"param1"
|
|
70
|
-
]
|
|
71
|
-
// controller method parameters (optional)
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
_When receiving a message:_
|
|
77
|
-
|
|
78
|
-
```json
|
|
79
|
-
{
|
|
80
|
-
"event": "event name",
|
|
81
|
-
"status": "OK"
|
|
82
|
-
// or "Error",
|
|
83
|
-
"message": "backend response"
|
|
84
|
-
// optional
|
|
85
|
-
}
|
|
86
|
-
```
|