From 69f4a758303dad029e379d555c4dabfbe513532c Mon Sep 17 00:00:00 2001 From: Jeremie Pardou-Piquemal <571533+jrmi@users.noreply.github.com> Date: Sat, 11 Sep 2021 14:52:21 +0200 Subject: [PATCH] Rename to wire.io --- .nvmrc | 2 +- Dockerfile | 4 +-- README.md | 21 +++++++------- package-lock.json | 31 ++++----------------- package.json | 8 +++--- src/client.js | 26 +++++++++--------- src/client.test.js | 68 +++++++++++++++++++++++----------------------- src/index.js | 6 ++-- src/server.js | 6 ++-- src/startServer.js | 4 +-- 10 files changed, 78 insertions(+), 98 deletions(-) diff --git a/.nvmrc b/.nvmrc index 1a2f5bd..b009dfb 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -lts/* \ No newline at end of file +lts/* diff --git a/Dockerfile b/Dockerfile index 5e24cb7..25f94d5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,8 +3,8 @@ FROM node:14 # Create app directory WORKDIR /usr/src/app -RUN npm install -g npm && npm install -g client2client.io@latest +RUN npm install -g npm && npm install -g wire.io@latest EXPOSE 4000 -CMD [ "client2client" ] +CMD [ "wire.io" ] diff --git a/README.md b/README.md index 93b846a..94c15ac 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# Client2client.io +# Wire.io -A web client to client communication channel that mimic [WAMP protocol](https://wamp-proto.org/). You can pub/sub event and make RPC calls. +A web client to client communication channel that mimic [WAMP protocol](https://wamp-proto.org/). You can pub/sub event and make RPC calls directly to other clients through a router. Way mush simpler and light than Autobahn and don't need a fat router when you need something less powerfull. It based on venerable and very usefull [socket.io](https://socket.io/). @@ -8,25 +8,25 @@ Compatible with 2.X and 3.X version of socket.io. ## Usage -Client2client.io rely on [socket.io](https://socket.io/) on server and client. +Wire.io rely on [socket.io](https://socket.io/) for server and client. Launch the server without installing with npx: ```sh -npx client2client.io # need npm v7 in order to work +npx wire.io # need npm v7 in order to work ``` ## Installation ```sh -npm install client2client.io +npm install wire.io ``` ### Serve side code ```js import express from 'express'; -import { handleC2C } from 'client2client'; +import { handleWire } from 'wire.io'; var app = express(); var http = require('http').createServer(app); @@ -41,9 +41,9 @@ http.listen(port, () => { io.on('connection', (socket) => { const options = { // This are defaults log: console.log; - logPrefix = '[c2c]' + logPrefix = '[Wire] ' } - handleC2C(socket, options); // Option is optionnal + handleWire(socket, options); // Option is optionnal }); ``` @@ -51,6 +51,7 @@ io.on('connection', (socket) => { ```js import io from 'socket.io-client'; +import { join } from 'wire.io'; const socket = io.connect("", { 'reconnection delay': 0, @@ -59,11 +60,11 @@ const socket = io.connect("", { }); // Create room object -const room = await join({ +const room = await joinWire({ socket: socket, // Socket io socket object room: 'test', // Room name onJoined = (room) => { // Callback when room is joined (Optionnal) - console.log("Connected to client2client server with id ", room.userId); + console.log("Connected to wire.io server with id ", room.userId); }, onMaster = (room) => { // Callback if the user is the room master i.e. the first user (on next if first quit). (Optionnal) console.log("You are now master of the room"); diff --git a/package-lock.json b/package-lock.json index b69c412..e7a24ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,18 @@ { - "name": "client2client.io", - "version": "2.0.3", + "name": "wire.io", + "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "2.0.3", + "version": "1.0.0", "license": "ISC", "dependencies": { "nanoid": "^3.1.20" }, "bin": { - "client2client": "lib/cli.es5.js", - "client2client.io": "lib/cli.es5.js" + "wire": "lib/cli.es5.js", + "wire.io": "lib/cli.es5.js" }, "devDependencies": { "@babel/cli": "^7.14.3", @@ -5043,20 +5043,6 @@ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", "dev": true }, - "node_modules/fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "dev": true, - "hasInstallScript": true, - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, "node_modules/function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", @@ -15736,13 +15722,6 @@ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", "dev": true }, - "fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "dev": true, - "optional": true - }, "function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", diff --git a/package.json b/package.json index 84e707d..888b61f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "client2client.io", - "version": "2.0.3", + "name": "wire.io", + "version": "1.0.0", "description": "Socket.io wrapper to enable client to client communications like RPC and pub/sub. Inspired by WAMP protocol.", "main": "lib/index.js", "keywords": [ @@ -11,8 +11,8 @@ "author": "Jérémie Pardou", "license": "ISC", "bin": { - "client2client": "lib/cli.es5.js", - "client2client.io": "lib/cli.es5.js" + "wire": "lib/cli.es5.js", + "wire.io": "lib/cli.es5.js" }, "scripts": { "clean": "rm ./lib/ -r", diff --git a/src/client.js b/src/client.js index b63e296..62ff678 100644 --- a/src/client.js +++ b/src/client.js @@ -1,6 +1,6 @@ import { nanoid } from 'nanoid'; -class Client2CLient { +class Wire { constructor(socket, room, userId = null) { this._socket = socket; this.userId = userId; @@ -111,58 +111,58 @@ class Client2CLient { } /** - * Join a client2client room. + * Join a wire.io room. * @param {socket} socket socket.io instance. * @param {string} name of the room * @param {function} onMaster is called when the client become the master of * the room, i.e. the first client or the next one if the first quit. * @param {function} onJoined is called on each connection, reconnection after - * client2client is initialized. + * wire.io is initialized. * @param {string} userId (optionnal) to force userId. */ -export const joinClient2Client = ({ +export const joinWire = ({ socket, room, onJoined = () => {}, onMaster = () => {}, userId = null, }) => { - const C2Croom = new Client2CLient(socket, room, userId); + const WireRoom = new Wire(socket, room, userId); return new Promise((resolve) => { // Avoid multiple join let waitForResponse = true; socket.on(`${room}.isMaster`, () => { - if (C2Croom._left) { + if (WireRoom._left) { return; } onMaster(room); }); socket.on(`${room}.roomJoined`, (userId) => { - if (C2Croom._left) { + if (WireRoom._left) { return; } - C2Croom.userId = userId; + WireRoom.userId = userId; waitForResponse = false; - onJoined(C2Croom); - resolve(C2Croom); + onJoined(WireRoom); + resolve(WireRoom); }); // Rejoin on reconnection socket.on('connect', () => { // If joined already called or room left // we quit - if (C2Croom._left || waitForResponse) { + if (WireRoom._left || waitForResponse) { return; } // Restore events with same userId socket.emit('joinSuperSocket', { room, - userId: C2Croom.userId, + userId: WireRoom.userId, }); }); socket.emit('joinSuperSocket', { room, userId }); }); }; -export default joinClient2Client; +export default joinWire; diff --git a/src/client.test.js b/src/client.test.js index da15a19..5fee2dd 100644 --- a/src/client.test.js +++ b/src/client.test.js @@ -1,5 +1,5 @@ import io from 'socket.io-client'; -import join from './client'; +import joinWire from './client'; import retry from 'retry-assert'; const port = process.env.PORT || 4000; @@ -53,15 +53,15 @@ describe('Client', () => { }); it('connect to room', async () => { - const room = await join({ socket: socket1, room: 'test' }); + const room = await joinWire({ socket: socket1, room: 'test' }); expect(room).toBeDefined(); expect(room.userId).toBeDefined(); expect(room.userId).not.toBe(null); }); it('should receive published event', async (done) => { - const room1 = await join({ socket: socket1, room: 'test' }); - const room2 = await join({ socket: socket2, room: 'test' }); + const room1 = await joinWire({ socket: socket1, room: 'test' }); + const room2 = await joinWire({ socket: socket2, room: 'test' }); room1.subscribe('testevent', (params) => { expect(params).toEqual({ test: 'test' }); done(); @@ -70,8 +70,8 @@ describe('Client', () => { }); it('should not receive published event if unsubscribed', async () => { - const room1 = await join({ socket: socket1, room: 'test' }); - const room2 = await join({ socket: socket2, room: 'test' }); + const room1 = await joinWire({ socket: socket1, room: 'test' }); + const room2 = await joinWire({ socket: socket2, room: 'test' }); const callback = jest.fn(); const unsubscribe = room1.subscribe('testevent', callback); @@ -83,29 +83,29 @@ describe('Client', () => { }); it('should receive incoming user event', async (done) => { - const room1 = await join({ socket: socket1, room: 'test' }); + const room1 = await joinWire({ socket: socket1, room: 'test' }); room1.subscribe('userEnter', (params) => { expect(params).toBeDefined(); done(); }); - const room2 = await join({ socket: socket2, room: 'test' }); + const room2 = await joinWire({ socket: socket2, room: 'test' }); }); it('should receive user leave event', async (done) => { - const room1 = await join({ socket: socket1, room: 'test' }); + const room1 = await joinWire({ socket: socket1, room: 'test' }); room1.subscribe('userLeave', (params) => { expect(params).toBeDefined(); done(); }); - const room2 = await join({ socket: socket2, room: 'test' }); + const room2 = await joinWire({ socket: socket2, room: 'test' }); socket2.disconnect(); }); it('should receive self published event', async (done) => { - const room1 = await join({ socket: socket1, room: 'test' }); + const room1 = await joinWire({ socket: socket1, room: 'test' }); room1.subscribe('testevent', (params) => { expect(params).toEqual({ test: 'test' }); done(); @@ -114,8 +114,8 @@ describe('Client', () => { }); it('should call remote function', async () => { - const room1 = await join({ socket: socket1, room: 'test' }); - const room2 = await join({ socket: socket2, room: 'test' }); + const room1 = await joinWire({ socket: socket1, room: 'test' }); + const room2 = await joinWire({ socket: socket2, room: 'test' }); await room1.register('testrpc', (params) => { expect(params).toEqual({ test: 'testa' }); @@ -128,8 +128,8 @@ describe('Client', () => { }); it('should call remote async function', async () => { - const room1 = await join({ socket: socket1, room: 'test' }); - const room2 = await join({ socket: socket2, room: 'test' }); + const room1 = await joinWire({ socket: socket1, room: 'test' }); + const room2 = await joinWire({ socket: socket2, room: 'test' }); await room1.register('testrpc', async (params) => { expect(params).toEqual({ test: 'testa' }); @@ -142,8 +142,8 @@ describe('Client', () => { }); it('should unregister/register remote function', async () => { - const room1 = await join({ socket: socket1, room: 'test' }); - const room2 = await join({ socket: socket2, room: 'test' }); + const room1 = await joinWire({ socket: socket1, room: 'test' }); + const room2 = await joinWire({ socket: socket2, room: 'test' }); const firstCallback = jest.fn(); @@ -169,7 +169,7 @@ describe('Client', () => { }); it('should not call not registered remote function', async (done) => { - const room2 = await join({ socket: socket2, room: 'test' }); + const room2 = await joinWire({ socket: socket2, room: 'test' }); try { await room2.call('testrpc', { test: 'testbis' }); @@ -180,8 +180,8 @@ describe('Client', () => { }); it('should call remote function with exception', async (done) => { - const room1 = await join({ socket: socket1, room: 'test' }); - const room2 = await join({ socket: socket2, room: 'test' }); + const room1 = await joinWire({ socket: socket1, room: 'test' }); + const room2 = await joinWire({ socket: socket2, room: 'test' }); await room1.register('testrpc', (params) => { throw new Error('test error'); @@ -199,17 +199,17 @@ describe('Client', () => { const onMaster2 = jest.fn(); const onMaster3 = jest.fn(); - const room1 = await join({ + const room1 = await joinWire({ socket: socket1, room: 'test', onMaster: onMaster1, }); - const room2 = await join({ + const room2 = await joinWire({ socket: socket2, room: 'test', onMaster: onMaster2, }); - const room3 = await join({ + const room3 = await joinWire({ socket: socket3, room: 'test', onMaster: onMaster3, @@ -242,7 +242,7 @@ describe('Client', () => { it('should call onJoin callback', async () => { const onJoin1 = jest.fn(); - const s = await join({ + const s = await joinWire({ socket: socket1, room: 'test', onJoined: onJoin1, @@ -254,7 +254,7 @@ describe('Client', () => { it('should force userId', async () => { const onJoin1 = jest.fn(); - const s = await join({ + const s = await joinWire({ socket: socket1, room: 'test', onJoined: onJoin1, @@ -265,9 +265,9 @@ describe('Client', () => { }); it('should receive published event only on same room', async () => { - const room1 = await join({ socket: socket1, room: 'test' }); - const room2 = await join({ socket: socket2, room: 'test' }); - const room3 = await join({ socket: socket3, room: 'test2' }); + const room1 = await joinWire({ socket: socket1, room: 'test' }); + const room2 = await joinWire({ socket: socket2, room: 'test' }); + const room3 = await joinWire({ socket: socket3, room: 'test2' }); const callback = jest.fn(); room1.subscribe('testevent', callback); @@ -287,9 +287,9 @@ describe('Client', () => { }); it('should not receive published event if room is left', async () => { - const room1 = await join({ socket: socket1, room: 'test' }); - const room2 = await join({ socket: socket2, room: 'test' }); - const room3 = await join({ socket: socket3, room: 'test' }); + const room1 = await joinWire({ socket: socket1, room: 'test' }); + const room2 = await joinWire({ socket: socket2, room: 'test' }); + const room3 = await joinWire({ socket: socket3, room: 'test' }); const callback = jest.fn(); room1.subscribe('testevent', callback); @@ -321,9 +321,9 @@ describe('Client', () => { }); it('should call remote async function only in same room', async (done) => { - const room1 = await join({ socket: socket1, room: 'test' }); - const room2 = await join({ socket: socket2, room: 'test' }); - const room3 = await join({ socket: socket2, room: 'test3' }); + const room1 = await joinWire({ socket: socket1, room: 'test' }); + const room2 = await joinWire({ socket: socket2, room: 'test' }); + const room3 = await joinWire({ socket: socket2, room: 'test3' }); await room1.register('testrpc', async (params) => { expect(params).toEqual({ test: 'testa' }); diff --git a/src/index.js b/src/index.js index acb147d..c184c73 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import handleC2C from './server'; -import join from './client'; +import handleWire from './server'; +import joinWire from './client'; -export { handleC2C, join }; +export { handleWire, joinWire }; diff --git a/src/server.js b/src/server.js index 85f3db5..9b40fb6 100644 --- a/src/server.js +++ b/src/server.js @@ -2,9 +2,9 @@ import { nanoid } from 'nanoid'; const rooms = {}; -export const handleC2C = ( +export const handleWire = ( socket, - { log = console.log, logPrefix = '[c2c] ' } = {} + { log = console.log, logPrefix = '[Wire.io] ' } = {} ) => { socket.on('joinSuperSocket', ({ room: roomName, userId: givenUserId }) => { socket.join(roomName); @@ -126,4 +126,4 @@ export const handleC2C = ( }); }; -export default handleC2C; +export default handleWire; diff --git a/src/startServer.js b/src/startServer.js index b3df9ba..ee4338f 100644 --- a/src/startServer.js +++ b/src/startServer.js @@ -3,7 +3,7 @@ import { createServer } from 'http'; import io from 'socket.io'; import cors from 'cors'; -import { handleC2C } from '.'; +import { handleWire } from '.'; const startServer = ({ socketIOConfig = {}, port = 4000 }) => { var app = express(); @@ -24,7 +24,7 @@ const startServer = ({ socketIOConfig = {}, port = 4000 }) => { }); ioServer.on('connection', (socket) => { - handleC2C(socket, { log: (msg) => console.log(msg) }); + handleWire(socket, { log: (msg) => console.log(msg) }); }); app.get('/', (req, res) => {