stateroom
Provides state synchronization for clients
Last updated 4 years ago by download .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install stateroom 
SYNC missed versions from official npm registry.

stateroom

Keeps a collection of stateful clients synchronized so that each is always updated with the current state of every client in the room.

Each client has it's own state to which it has read/write access. It also has read access to every other client's state.

Install

npm i stateroom

Server Example

import WebsocketClient from 'stateroom/server/clients/websocket';
import {createRoom, createRoomManager} from 'stateroom';
import {Server} from 'ws';


const room = createRoom();
const wss = new Server();

wss.on('connection', ws => {
    const client = new WebsocketClient(ws); // Adapter for websocket connections

    ws.on('close', () => room.removeClient(client));
    room.addClient(client);
});

// If you want to have multiple rooms, RoomManager is a convenience class
const rooms = createRoomManager();

wss.on('connection', ws => {
    const roomname = 'room' + Math.random(); // Pretend the user picked this
    const room = rooms.get(roomname);
    const client = new WebsocketClient(ws); // Adapter for websocket connections

    ws.on('close', () => room.removeClient(client));
    room.addClient(client);

    // The room will be culled automatically when the last user leaves
});

Client Example

// At the moment, only WebSocket is supported by the client library
import {createClient} from 'stateroom/client';


const ws = new WebSocket(serverUrl);
const client = createClient(ws);

client.id; // Contains your id
client.set('prop1', 5);
client.setOwnState({prop: 4}); // Replaces entire state
client.delete('prop');
client.clear(); // Clears all properties
client.getState(); // {yourid: {}, otheruserid: {someprop: 'test'}}

client.subscribe(() => { // On any change
    client.getState(); // Do something with the new state
});

License: MIT

Current Tags

  • 2.0.0                                ...           latest (4 years ago)

8 Versions

  • 2.0.0                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
  • 0.0.5                                ...           5 years ago
  • 0.0.4                                ...           5 years ago
  • 0.0.3                                ...           5 years ago
  • 0.0.2                                ...           5 years ago
  • 0.0.1                                ...           5 years ago
  • 0.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (3)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |