Skip to content

Commit

Permalink
Rename to wire.io
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmi committed Sep 11, 2021
1 parent 64d9f60 commit 69f4a75
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lts/*
lts/*
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
# 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/).

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);
Expand All @@ -41,16 +41,17 @@ 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
});
```

## Client side code

```js
import io from 'socket.io-client';
import { join } from 'wire.io';

const socket = io.connect("<socket server url>", {
'reconnection delay': 0,
Expand All @@ -59,11 +60,11 @@ const socket = io.connect("<socket server url>", {
});

// 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");
Expand Down
31 changes: 5 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -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",
Expand Down
26 changes: 13 additions & 13 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { nanoid } from 'nanoid';

class Client2CLient {
class Wire {
constructor(socket, room, userId = null) {
this._socket = socket;
this.userId = userId;
Expand Down Expand Up @@ -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;
Loading

0 comments on commit 69f4a75

Please sign in to comment.