2.0.0 • Published 8 years ago

stateroom v2.0.0

Weekly downloads
10
License
MIT
Repository
github
Last release
8 years ago

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

2.0.0

8 years ago

1.0.0

8 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago

0.0.0

9 years ago