Skip to content

Commit

Permalink
Update deps. Bump version.
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-at-work committed Oct 28, 2022
1 parent 5d90ac7 commit 502d781
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 392 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## ⚠️ **Warning**
**This is using socket.io v2, newer versions of socket.io have their own library: https://github.com/socketio/socket.io-postgres-adapter. That should be used when socket.io is upgraded.**

# socket.io-postgres

socket.io-postgres allows you to communicate with socket.io servers easily from non-socket.io processes.

## How to use
Expand Down
38 changes: 10 additions & 28 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,55 @@ Object.defineProperty(exports, "__esModule", {
});
exports.PostgreSQLAdapter = void 0;
exports.default = createAdapter;

require("core-js/modules/es.regexp.flags.js");

var _pgPubsub = _interopRequireDefault(require("pg-pubsub"));

var _uuid = require("uuid");

var _socket = require("socket.io-adapter");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function createAdapter(connection, opts) {
return function (nsp) {
return new PostgreSQLAdapter(nsp, connection, opts);
};
}

class PostgreSQLAdapter extends _socket.Adapter {
constructor(nsp, connection, opts = {}) {
super(nsp);
this.pg = new _pgPubsub.default(connection);
this.uid = (0, _uuid.v4)();
this.prefix = opts.prefix || 'socket-io';

const init = async () => {
const channel = `${this.prefix}:${nsp.name}`;

try {
await this.pg.addChannel(channel, this.onmessage.bind(this));
} catch (err) {
console.error(`Error adding channel listener for "${channel}"`, err);
}
};

init();
}

onmessage(msg) {
// console.log(`onmessage()`, msg);

// ignore its own messages
if (msg.uid === this.uid) return;
const packet = msg.packet;
const options = {
rooms: new Set(msg.options.rooms),
except: new Set(msg.options.except),
flags: msg.options.flags
}; // default namespace
};

packet.nsp = packet.nsp || '/'; // ignore message for different namespace
// default namespace
packet.nsp = packet.nsp || '/';

// ignore message for different namespace
if (packet.nsp !== this.nsp.name) return;
super.broadcast(packet, options);
}

broadcast(packet, options, remote) {
// console.log('broadcast()', packet, options);
super.broadcast(packet, options);
packet.nsp = packet.nsp || '/';

if (!remote) {
const msg = {
uid: this.uid,
Expand All @@ -74,7 +64,6 @@ class PostgreSQLAdapter extends _socket.Adapter {
flags: options.flags
}
};

if (options.rooms.size > 0) {
options.rooms.forEach(room => {
// console.log(`broadcast to room: '${this.prefix}:${packet.nsp}:${room}'`, msg);
Expand All @@ -85,66 +74,59 @@ class PostgreSQLAdapter extends _socket.Adapter {
}
}
}

async add(id, room) {
const channel = `${this.prefix}:${this.nsp.name}:${room}`;

try {
await this.pg.addChannel(channel, this.onmessage.bind(this));
} catch (err) {
console.error(`Error adding channel listener for "${channel}"`, err);
}
}

async addAll(id, rooms) {
// console.log(`addAll(${id}, ${rooms})`);

if (!rooms) {
// console.log('no rooms');
return;
}

super.addAll(id, rooms);
let promises = [];
rooms.forEach(room => promises.push(this.add(id, room)));
await Promise.all(promises).catch(err => {
console.error(`Error adding channel listener for "${channel}"`, err);
});
}

async del(id, room) {
// console.log(`del(${id}, ${room})`);

if (!this.rooms.has(room)) {
// console.log('didnt have room');
return;
}

await this.pg.removeChannel(`${this.prefix}:${this.nsp.name}:${room}`, this.onmessage.bind(this));
super.del(id, room);
}

async delAll(id) {
// console.log(`delAll(${id})`);
const rooms = this.sids.get(id);

if (!rooms) {
// console.log('no rooms for sid', id);
return;
}

let promises = [];
rooms.forEach(room => promises.push(this.del(id, room)));
await Promise.all(promises).catch(err => {
console.error('Error removing multiple channel listeners', err);
});
; // await super.delAll(id);
;

// await super.delAll(id);
}

async close() {
try {
await this.pg.close();
} catch (err) {}
}

}

exports.PostgreSQLAdapter = PostgreSQLAdapter;
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
{
"name": "@palmetto/socket.io-postgres",
"version": "2.2.0",
"version": "2.3.0",
"description": "PostgreSQL adapter for providing cluster aware messaging with socket.io",
"main": "index.js",
"directories": {
"test": "test"
},
"dependencies": {
"core-js": "^3.26.0",
"pg-pubsub": "^0.8.0",
"socket.io-adapter": "~2.2.0",
"uuid": "^8.3.2"
"uuid": "^9.0.0"
},
"peerDependencies": {
"socket.io": "^3.1.0"
},
"devDependencies": {
"@babel/cli": "^7.18.9",
"@babel/core": "^7.18.9",
"@babel/preset-env": "^7.18.9",
"@babel/cli": "^7.19.3",
"@babel/core": "^7.19.6",
"@babel/preset-env": "^7.19.4",
"expect.js": "^0.3.1",
"mocha": "^10.0.0",
"mocha": "^10.1.0",
"socket.io": "^3.1.0",
"socket.io-client": "^3.1.0"
},
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('socket.io-postgres', function () {
});

it('broadcasts to rooms', function (done) {
// this.timeout(5000);
create(function (server1, client1) {
create(function (server2, client2) {
server1.on('connection', function (c1) {
Expand All @@ -34,13 +35,13 @@ describe('socket.io-postgres', function () {

server2.on('connection', function (c2) {
// does not join, performs broadcast
c2.broadcast.to('woot').emit('broadcast', { a: 'b' });
c2.broadcast.to('woot').emit('broadcast', 'message');
});

client1.on('broadcast', function () {
client1.disconnect();
client2.disconnect();
done(); // This gets called as far as I can tell but the test still fails.
done(); // This gets called as far as I can tell but the test still fails.

// setTimeout(function () {
// }, 100);
Expand All @@ -49,7 +50,6 @@ describe('socket.io-postgres', function () {
client2.on('broadcast', function () {
throw new Error('Not in room');
});

});
});
});
Expand Down
Loading

0 comments on commit 502d781

Please sign in to comment.