@sridhardvvce/gcp-pubsub-adapter 0.1.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/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2024 The Socket.IO team
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Socket.IO Google Cloud pub/sub adapter
2
+
3
+ The `@socket.io/gcp-pubsub-adapter` package allows broadcasting packets between multiple Socket.IO servers.
4
+
5
+ **Table of contents**
6
+
7
+ - [Supported features](#supported-features)
8
+ - [Installation](#installation)
9
+ - [Usage](#usage)
10
+ - [Options](#options)
11
+ - [License](#license)
12
+
13
+ ## Supported features
14
+
15
+ | Feature | `socket.io` version | Support |
16
+ |---------------------------------|---------------------|------------------------------------------------|
17
+ | Socket management | `4.0.0` | :white_check_mark: YES (since version `0.1.0`) |
18
+ | Inter-server communication | `4.1.0` | :white_check_mark: YES (since version `0.1.0`) |
19
+ | Broadcast with acknowledgements | `4.5.0` | :white_check_mark: YES (since version `0.1.0`) |
20
+ | Connection state recovery | `4.6.0` | :x: NO |
21
+
22
+ ## Installation
23
+
24
+ ```
25
+ npm install @socket.io/gcp-pubsub-adapter
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```js
31
+ import { PubSub } from "@google-cloud/pubsub";
32
+ import { Server } from "socket.io";
33
+ import { createAdapter } from "@socket.io/gcp-pubsub-adapter";
34
+
35
+ const pubsub = new PubSub({
36
+ projectId: "your-project-id"
37
+ });
38
+
39
+ const topic = pubsub.topic(topicNameOrId);
40
+
41
+ const io = new Server({
42
+ adapter: createAdapter(topic)
43
+ });
44
+
45
+ // wait for the creation of the pub/sub subscription
46
+ await io.of("/").adapter.init();
47
+
48
+ io.listen(3000);
49
+ ```
50
+
51
+ ## Options
52
+
53
+ | Name | Description | Default value |
54
+ |-----------------------|-------------------------------------------------------------------------------------------------------------------|----------------|
55
+ | `subscriptionPrefix` | The prefix for the new subscription to create. | `socket.io` |
56
+ | `subscriptionOptions` | The options used to create the subscription. | `-` |
57
+ | `heartbeatInterval` | The number of ms between two heartbeats. | `5_000` |
58
+ | `heartbeatTimeout` | The number of ms without heartbeat before we consider a node down. | `10_000` |
59
+
60
+ ## License
61
+
62
+ [MIT](LICENSE)
@@ -0,0 +1,39 @@
1
+ import { ClusterAdapterWithHeartbeat } from "socket.io-adapter";
2
+ import type { ClusterAdapterOptions, ClusterMessage, ClusterResponse, Offset, ServerId } from "socket.io-adapter";
3
+ import type { Topic, Message, CreateSubscriptionOptions } from "@google-cloud/pubsub";
4
+ export interface AdapterOptions extends ClusterAdapterOptions {
5
+ /**
6
+ * The prefix for the new subscription to create
7
+ * @default "socket.io"
8
+ */
9
+ subscriptionPrefix?: string;
10
+ /**
11
+ * The options used to create the subscription.
12
+ */
13
+ subscriptionOptions?: CreateSubscriptionOptions;
14
+ }
15
+ /**
16
+ * Returns a function that will create a {@link PubSubAdapter} instance.
17
+ *
18
+ * @param topic - a Google pub/sub topic
19
+ * @param opts - additional options
20
+ *
21
+ * @public
22
+ */
23
+ export declare function createAdapter(topic: Topic, opts?: AdapterOptions): (nsp: any) => PubSubAdapter;
24
+ export declare class PubSubAdapter extends ClusterAdapterWithHeartbeat {
25
+ private readonly topic;
26
+ /**
27
+ * Adapter constructor.
28
+ *
29
+ * @param nsp - the namespace
30
+ * @param topic - a Google pub/sub topic
31
+ * @param opts - additional options
32
+ *
33
+ * @public
34
+ */
35
+ constructor(nsp: any, topic: Topic, opts: ClusterAdapterOptions);
36
+ protected doPublish(message: ClusterMessage): Promise<Offset>;
37
+ protected doPublishResponse(requesterUid: ServerId, response: ClusterResponse): Promise<void>;
38
+ onRawMessage(rawMessage: Message): void;
39
+ }
package/dist/index.js ADDED
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PubSubAdapter = exports.createAdapter = void 0;
4
+ const socket_io_adapter_1 = require("socket.io-adapter");
5
+ const msgpack_1 = require("@msgpack/msgpack");
6
+ const node_crypto_1 = require("node:crypto");
7
+ const debug = require("debug")("socket.io-gcloud-pubsub-adapter");
8
+ function randomId() {
9
+ return (0, node_crypto_1.randomBytes)(8).toString("hex");
10
+ }
11
+ /**
12
+ * Returns a function that will create a {@link PubSubAdapter} instance.
13
+ *
14
+ * @param topic - a Google pub/sub topic
15
+ * @param opts - additional options
16
+ *
17
+ * @public
18
+ */
19
+ function createAdapter(topic, opts = {}) {
20
+ const subscriptionPrefix = opts.subscriptionPrefix || "socket.io";
21
+ const subscriptionName = `${subscriptionPrefix}-${randomId()}`;
22
+ const namespaceToAdapters = new Map();
23
+ debug("creating subscription [%s]", subscriptionName);
24
+ // a new subscription is created every time, in order to always start at the last offset (and not replay old messages)
25
+ const subscriptionCreation = topic
26
+ .createSubscription(subscriptionName, opts.subscriptionOptions)
27
+ .then((res) => {
28
+ debug("subscription [%s] was successfully created", subscriptionName);
29
+ const subscription = res[0];
30
+ subscription.on("message", (message) => {
31
+ var _a;
32
+ const namespace = message.attributes["nsp"];
33
+ (_a = namespaceToAdapters.get(namespace)) === null || _a === void 0 ? void 0 : _a.onRawMessage(message);
34
+ message.ack();
35
+ });
36
+ subscription.on("error", (err) => {
37
+ debug("an error has occurred: %s", err.message);
38
+ });
39
+ })
40
+ .catch((err) => {
41
+ debug("an error has occurred while creating the subscription: %s", err.message);
42
+ throw err; // ✅ FAIL FAST //Sridhar
43
+ });
44
+ return function (nsp) {
45
+ const adapter = new PubSubAdapter(nsp, topic, opts);
46
+ namespaceToAdapters.set(nsp.name, adapter);
47
+ const defaultInit = adapter.init;
48
+ adapter.init = () => {
49
+ return subscriptionCreation.then(() => {
50
+ defaultInit.call(adapter);
51
+ });
52
+ };
53
+ const defaultClose = adapter.close;
54
+ adapter.close = () => {
55
+ namespaceToAdapters.delete(nsp.name);
56
+ if (namespaceToAdapters.size === 0) {
57
+ debug("deleting subscription [%s]", subscriptionName);
58
+ topic
59
+ .subscription(subscriptionName)
60
+ .delete()
61
+ .then(() => {
62
+ debug("subscription [%s] was successfully deleted", subscriptionName);
63
+ })
64
+ .catch((err) => {
65
+ debug("an error has occurred while deleting the subscription: %s", err.message);
66
+ });
67
+ }
68
+ defaultClose.call(adapter);
69
+ };
70
+ return adapter;
71
+ };
72
+ }
73
+ exports.createAdapter = createAdapter;
74
+ class PubSubAdapter extends socket_io_adapter_1.ClusterAdapterWithHeartbeat {
75
+ /**
76
+ * Adapter constructor.
77
+ *
78
+ * @param nsp - the namespace
79
+ * @param topic - a Google pub/sub topic
80
+ * @param opts - additional options
81
+ *
82
+ * @public
83
+ */
84
+ constructor(nsp, topic, opts) {
85
+ super(nsp, opts);
86
+ this.topic = topic;
87
+ }
88
+ doPublish(message) {
89
+ return this.topic
90
+ .publishMessage({
91
+ data: Buffer.from((0, msgpack_1.encode)(message)),
92
+ attributes: {
93
+ nsp: this.nsp.name,
94
+ uid: this.uid,
95
+ },
96
+ })
97
+ .then();
98
+ }
99
+ doPublishResponse(requesterUid, response) {
100
+ return this.topic
101
+ .publishMessage({
102
+ data: Buffer.from((0, msgpack_1.encode)(response)),
103
+ attributes: {
104
+ nsp: this.nsp.name,
105
+ uid: this.uid,
106
+ requesterUid,
107
+ },
108
+ })
109
+ .then();
110
+ }
111
+ onRawMessage(rawMessage) {
112
+ if (rawMessage.attributes["uid"] === this.uid) {
113
+ debug("ignore message from self");
114
+ return;
115
+ }
116
+ const requesterUid = rawMessage.attributes["requesterUid"];
117
+ if (requesterUid && requesterUid !== this.uid) {
118
+ debug("ignore response for another node");
119
+ return;
120
+ }
121
+ const decoded = (0, msgpack_1.decode)(rawMessage.data);
122
+ debug("received %j", decoded);
123
+ if (requesterUid) {
124
+ this.onResponse(decoded);
125
+ }
126
+ else {
127
+ this.onMessage(decoded);
128
+ }
129
+ }
130
+ }
131
+ exports.PubSubAdapter = PubSubAdapter;
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@sridhardvvce/gcp-pubsub-adapter",
3
+ "version": "0.1.0",
4
+ "description": "The Socket.IO adapter for Google Cloud pub/sub, allowing to broadcast events between several Socket.IO servers",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git@github.com:socketio/socket.io-gcp-pubsub-adapter.git"
9
+ },
10
+ "files": [
11
+ "dist/"
12
+ ],
13
+ "main": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "scripts": {
16
+ "compile": "rimraf ./dist && tsc",
17
+ "test": "npm run format:check && tsc && nyc mocha --require ts-node/register --timeout 10000 test/index.ts",
18
+ "format:check": "prettier --parser typescript --check 'lib/**/*.ts' 'test/**/*.ts'",
19
+ "format:fix": "prettier --parser typescript --write 'lib/**/*.ts' 'test/**/*.ts'",
20
+ "prepack": "npm run compile"
21
+ },
22
+ "dependencies": {
23
+ "@google-cloud/pubsub": "^4.3.3",
24
+ "@msgpack/msgpack": "^2.8.0",
25
+ "debug": "~4.3.4"
26
+ },
27
+ "peerDependencies": {
28
+ "socket.io-adapter": "^2.5.4"
29
+ },
30
+ "devDependencies": {
31
+ "@types/expect.js": "^0.3.29",
32
+ "@types/mocha": "^10.0.6",
33
+ "@types/node": "^14.14.7",
34
+ "expect.js": "0.3.1",
35
+ "mocha": "^10.2.0",
36
+ "nyc": "^15.1.0",
37
+ "prettier": "^2.1.2",
38
+ "rimraf": "^5.0.5",
39
+ "socket.io": "^4.6.1",
40
+ "socket.io-client": "^4.6.1",
41
+ "ts-node": "^10.9.1",
42
+ "typescript": "^4.9.4"
43
+ },
44
+ "engines": {
45
+ "node": ">=18.0.0"
46
+ },
47
+ "keywords": [
48
+ "socket.io",
49
+ "google cloud platform",
50
+ "gcp",
51
+ "pubsub"
52
+ ]
53
+ }