Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Mar 19, 2024
0 parents commit 886bbe6
Show file tree
Hide file tree
Showing 10 changed files with 5,120 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI

on:
push:
pull_request:
schedule:
- cron: '0 0 * * 0'

permissions:
contents: read

jobs:
test-node:
runs-on: ubuntu-latest
timeout-minutes: 10

strategy:
matrix:
node-version:
- 20

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm ci

- name: Run tests
# would require GCP credentials or the emulator (https://cloud.google.com/pubsub/docs/emulator)
# run: npm test
run: npm run format:check && npm run compile
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

npm-debug.log
node_modules
.idea
.nyc_output/
dist/
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2024 The Socket.IO team

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Socket.IO Google Cloud pub/sub adapter

The `@socket.io/gcp-pubsub-adapter` package allows broadcasting packets between multiple Socket.IO servers.

**Table of contents**

- [Supported features](#supported-features)
- [Installation](#installation)
- [Usage](#usage)
- [Options](#options)
- [License](#license)

## Supported features

| Feature | `socket.io` version | Support |
|---------------------------------|---------------------|------------------------------------------------|
| Socket management | `4.0.0` | :white_check_mark: YES (since version `0.1.0`) |
| Inter-server communication | `4.1.0` | :white_check_mark: YES (since version `0.1.0`) |
| Broadcast with acknowledgements | `4.5.0` | :white_check_mark: YES (since version `0.1.0`) |
| Connection state recovery | `4.6.0` | :x: NO |

## Installation

```
npm install @socket.io/gcp-pubsub-adapter
```

## Usage

```js
import { PubSub } from "@google-cloud/pubsub";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/gcp-pubsub-adapter";

const pubsub = new PubSub({
projectId: "your-project-id"
});

const topic = pubsub.topic(topicNameOrId);

const io = new Server({
adapter: createAdapter(topic)
});

// wait for the creation of the pub/sub subscription
await io.of("/").adapter.init();

io.listen(3000);
```

## Options

| Name | Description | Default value |
|-----------------------|-------------------------------------------------------------------------------------------------------------------|----------------|
| `subscriptionPrefix` | The prefix for the new subscription to create. | `socket.io` |
| `subscriptionOptions` | The options used to create the subscription. | `-` |
| `heartbeatInterval` | The number of ms between two heartbeats. | `5_000` |
| `heartbeatTimeout` | The number of ms without heartbeat before we consider a node down. | `10_000` |

## License

[MIT](LICENSE)
187 changes: 187 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import { ClusterAdapterWithHeartbeat } from "socket.io-adapter";
import type {
ClusterAdapterOptions,
ClusterMessage,
ClusterResponse,
Offset,
ServerId,
} from "socket.io-adapter";
import { encode, decode } from "@msgpack/msgpack";
import type {
Topic,
Message,
CreateSubscriptionOptions,
} from "@google-cloud/pubsub";
import { randomBytes } from "node:crypto";

const debug = require("debug")("socket.io-gcloud-pubsub-adapter");

function randomId() {
return randomBytes(8).toString("hex");
}

export interface AdapterOptions extends ClusterAdapterOptions {
/**
* The prefix for the new subscription to create
* @default "socket.io"
*/
subscriptionPrefix?: string;
/**
* The options used to create the subscription.
*/
subscriptionOptions?: CreateSubscriptionOptions;
}

/**
* Returns a function that will create a {@link PubSubAdapter} instance.
*
* @param topic - a Google pub/sub topic
* @param opts - additional options
*
* @public
*/
export function createAdapter(topic: Topic, opts: AdapterOptions = {}) {
const subscriptionPrefix = opts.subscriptionPrefix || "socket.io";
const subscriptionName = `${subscriptionPrefix}-${randomId()}`;

const namespaceToAdapters = new Map<string, PubSubAdapter>();

debug("creating subscription [%s]", subscriptionName);

// a new subscription is created every time, in order to always start at the last offset (and not replay old messages)
const subscriptionCreation = topic
.createSubscription(subscriptionName, opts.subscriptionOptions)
.then((res) => {
debug("subscription [%s] was successfully created", subscriptionName);
const subscription = res[0];

subscription.on("message", (message) => {
const namespace = message.attributes["nsp"];

namespaceToAdapters.get(namespace)?.onRawMessage(message);

message.ack();
});

subscription.on("error", (err) => {
debug("an error has occurred: %s", err.message);
});
})
.catch((err) => {
debug(
"an error has occurred while creating the subscription: %s",
err.message
);
});

return function (nsp: any) {
const adapter = new PubSubAdapter(nsp, topic, opts);

namespaceToAdapters.set(nsp.name, adapter);

const defaultInit = adapter.init;

adapter.init = () => {
return subscriptionCreation.then(() => {
defaultInit.call(adapter);
});
};

const defaultClose = adapter.close;

adapter.close = () => {
namespaceToAdapters.delete(nsp.name);

if (namespaceToAdapters.size === 0) {
debug("deleting subscription [%s]", subscriptionName);

topic
.subscription(subscriptionName)
.delete()
.then(() => {
debug(
"subscription [%s] was successfully deleted",
subscriptionName
);
})
.catch((err) => {
debug(
"an error has occurred while deleting the subscription: %s",
err.message
);
});
}

defaultClose.call(adapter);
};

return adapter;
};
}

export class PubSubAdapter extends ClusterAdapterWithHeartbeat {
private readonly topic: Topic;
/**
* Adapter constructor.
*
* @param nsp - the namespace
* @param topic - a Google pub/sub topic
* @param opts - additional options
*
* @public
*/
constructor(nsp: any, topic: Topic, opts: ClusterAdapterOptions) {
super(nsp, opts);
this.topic = topic;
}

protected doPublish(message: ClusterMessage): Promise<Offset> {
return this.topic
.publishMessage({
data: Buffer.from(encode(message)),
attributes: {
nsp: this.nsp.name,
uid: this.uid,
},
})
.then();
}

protected doPublishResponse(
requesterUid: ServerId,
response: ClusterResponse
): Promise<void> {
return this.topic
.publishMessage({
data: Buffer.from(encode(response)),
attributes: {
nsp: this.nsp.name,
uid: this.uid,
requesterUid,
},
})
.then();
}

public onRawMessage(rawMessage: Message) {
if (rawMessage.attributes["uid"] === this.uid) {
debug("ignore message from self");
return;
}

const requesterUid = rawMessage.attributes["requesterUid"];
if (requesterUid && requesterUid !== this.uid) {
debug("ignore response for another node");
return;
}

const decoded = decode(rawMessage.data);
debug("received %j", decoded);

if (requesterUid) {
this.onResponse(decoded as ClusterResponse);
} else {
this.onMessage(decoded as ClusterMessage);
}
}
}
Loading

0 comments on commit 886bbe6

Please sign in to comment.