![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Framework to manage distributed WebRTC servers that communicate with browser clients
A framework to manage distributed WebRTC servers that communicate with browser clients.
It has been created to be used by JumpSuit. It is generally great for web-based games, but I am sure you will find other uses.
Basically, you have:
I recommend reading this blog article to understand the motivation behind Enslavism.
$ yarn install # or npm install
$ yarn watch # or npm run watch
$ node examples/master.js # in a second terminal
$ node examples/slave.js # in a third terminal
Now open your browser at http://localhost:8081/
.
If you modify server code, you have to run node bundler.js
to bundle your changes.
If you modify client code, the master takes care of re-bundling for you in development environment. In production (i.e. $NODE_ENV
is set to production
) you have to restart the master.
You need to include /enslavism/client.js
in your HTML document like so:
<script src="/enslavism/client.js"></script>
let masterCon = new MasterConnection('ws://localhost:8081');
An array of received slaves.
slaveCo
: SlaveConnectionTriggered when a new slave is received. The slave will be available in the slave list.
masterCon.addEventListener('slaveadded', slaveCo => {
console.log('new slave', slaveCo);
});
slaveCo
: SlaveConnectionTriggered when a slave is removed from the slave list. Calling any of slaveCo
's methods won't work.
masterCon.addEventListener('slaveremoved', slaveCo => {
console.log('slave has been removed', slaveCo);
console.log('here was its id', slaveCo.id);
console.log('here was its userData', slaveCo.userData);
});
Triggered when a slave the client attempted to connect to rejected the connection.
slaveCo.addEventListener('rejected', () => {
console.log('The slave has rejected the connection :-(');
});
Connect to a slave.
Close the connection. It can be reopened by calling slaveConnection.connect()
again.
Note that all the DataChannel
s that were opened from this SlaveConnection
will be closed.
Create a new data channel. Returns a Promise that resolves with the data channel.
Will connect the client if it isn't yet. As connecting takes time, if you are able to anticipate a connection but not which data channel to open, you can call slaveConnection.connect()
before.
slaveCo.createDataChannel('test').then(dc => {
dc.addEventListener('message', msg => {
console.log(msg);
});
dc.send('What have I wrought!');
});
dcOptions
is an Object
which can contain the following properties.
The ordered
property is known to work. You might want to check out this upstream issue regarding the other properties.
Create an Enslavism master.
const Master = require('enslavism').Master;
let myMaster = new Master(8080); // creates master listening on port 8080
const Master = require('enslavism').Master,
http = require('http');
let myServer = http.createServer((req, res) => {
res.end('Hello world');
});
myServer.listen(8081);
let myMaster = new Master(myServer);
authData
: Objectreject
: FunctionTriggered when a slave wants to connect.
By default, the connection is accepted. If reject
is called, the connection will be rejected. reject
accepts a string as an optional argument which is the reason the connection was rejected.
authData
is the data provided in the slave constructor.
myMaster.on('slaveauth', (authData, reject) => {
if (authData.username !== 'getkey' || authData.password !== 'secret') reject('Invalid credentials!');
});
authData
: Objectreject
: FunctionTriggered when a client wants to connect.
By default, the connection is accepted. If reject
is called, the connection will be rejected. reject
accepts a string as an optional argument which is the reason the connection was rejected.
authData
is an object containing the cookies set by the client.
myMaster.on('clientauth', (authData, reject) => {
if (authData.username !== undefined) console.log(authData.username + " wants to connect!");
});
ws
: WebSocketTriggered once a slave is connected.
myMaster.on('slaveconnection', ws => {
console.log('Slave connected @', ws._socket.remoteAddress);
});
ws
: WebSocketTriggered once a client is connected.
myMaster.on('clientconnection', ws => {
console.log('Client connected @', ws._socket.remoteAddress);
});
err
: ErrorTriggered when an error occurs on the underlying server.
myMaster.on('error', err => {
console.log(err);
});
Returns a promise that resolves with an enslavism.Slave
.
userData
will be available to all clients and can contain any JavaScript value.
The optional argument authData
in an object containing strings. It may be used to authenticate slaves.
new enslavism.Slave('ws://localhost:8081', {
name: 'my slave server',
connectedAmount: 16
}).then(slave => {
console.log('Succesfully connected to the master:', slave);
}).catch(err => {
console.log('Couldn\'t connect to the master:', err);
});
reject
: FunctionTriggered each time a client wants to connect.
If reject
is called, the connection will be rejected.
slave.on('offer', reject => {
if (connectedClientAmount > 10) reject();
});
clientCo
: enslavism.ClientConnectionTriggered each time a client connects.
slave.on('connection', clientCo => {
console.log(clientCo);
});
dc
: DataChannelTriggered each time a client creates a datachannel.
clientCo.on('datachannel', dc => {
console.log('new dataChannel', dc);
dc.on('open', ev => { // triggered once the datachannel is open
console.log('data channel open', ev);
dc.send('hallo welt');
});
dc.on('message', msg => { // triggered when receiving a message from a client
console.log(msg);
});
});
FAQs
Framework to manage distributed WebRTC servers that communicate with browser clients
The npm package enslavism receives a total of 13 weekly downloads. As such, enslavism popularity was classified as not popular.
We found that enslavism demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.