A web client to client communication channel that mimic WAMP protocol. You can pub/sub event and make RPC calls. 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.
Compatible with 2.X and 3.X version of socket.io.
npm install client2client.io
Client2client rely on socket.io on server and client.
import express from 'express';
import { handleC2C } from 'client2client';
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
const port = process.env.PORT || 4000;
http.listen(port, () => {
console.log(`listening on *:${port}`);
});
io.on('connection', (socket) => {
const options = { // This are defaults
log: console.log;
logPrefix = '[c2c]'
}
handleC2C(socket, options); // Option is optionnal
});
import io from 'socket.io-client';
const socket = io.connect("<socket server url>", {
'reconnection delay': 0,
'reopen delay': 0,
forceNew: true,
});
// Create room object
const room = await join({
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);
},
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");
},
userId: "myuserid" // To force user id (Optionnal)
});
All calls are client side. Since you have the room instance you can comunicate with other client with this API.
Send an event to all other clients in room. Also to self if true.
params can be any js type that is serializable.
Subscribe the callback to an event. The callback receive the params data if any.
Register a function to be called by other clients. If any client use .call with the same function name,
the callback will be called with the given parameters.
Call a previously registered function. Return the call result.
First install dependencies:
npm ci
Start the server in watch mode:
npm run dev
Then run tests:
npm test
Content type
Image
Digest
Size
383.4 MB
Last updated
over 5 years ago
docker pull jrmi/client2client.io