1 | # Causal Tree Server Socket.io
|
2 |
|
3 | [![npm (scoped)](https://img.shields.io/npm/v/@casual-simulation/causal-tree-server-socketio.svg)](https://www.npmjs.com/package/@casual-simulation/causal-tree-server-socketio)
|
4 |
|
5 | Casual trees served over socket.io.
|
6 |
|
7 | ## Installation
|
8 |
|
9 | ```
|
10 | npm install @casual-simulation/causal-tree-server-socketio
|
11 | ```
|
12 |
|
13 | ## Usage
|
14 |
|
15 | #### Start a Causal Tree Server
|
16 |
|
17 | ```typescript
|
18 | import * as Http from 'http';
|
19 | import SocketIO from 'socket.io';
|
20 | import { MongoClient } from 'mongodb';
|
21 | import { CausalTreeServerSocketIO } from '@casual-simulation/causal-tree-server-socketio';
|
22 | import { CausalTreeStoreMongoDB } from '@casual-simulation/causal-tree-store-mongodb';
|
23 | import { DeviceManagerImpl, ChannelManagerImpl, NullDeviceAuthenticator, NullChannelAuthorizer } from '@casual-simulation/causal-tree-server';
|
24 | import { CausalTreeFactory, CausalTree } from '@casual-simulation/causal-trees';
|
25 |
|
26 | demo();
|
27 |
|
28 | async function demo() {
|
29 |
|
30 | // Setup the HTTP server and Socket.io Server.
|
31 | const http = new Http.Server();
|
32 | const socket = SocketIO(this._http);
|
33 |
|
34 | // The claims and roles that the server
|
35 | // should have.
|
36 | // This is like a user except it bypasses authentication
|
37 | // and role lookup and just lets the server act like it
|
38 | // has the given claims and roles.
|
39 | const serverDevice: DeviceInfo = {
|
40 | claims: {
|
41 | [USERNAME_CLAIM]: 'server',
|
42 | [DEVICE_ID_CLAIM]: 'server',
|
43 | [SESSION_ID_CLAIM]: 'server',
|
44 | },
|
45 | roles: [SERVER_ROLE],
|
46 | };
|
47 |
|
48 | // The factory that should be used to create causal trees
|
49 | // from stored data.
|
50 | const causalTreeFactory = new CausalTreeFactory({
|
51 | test: (stored, options) => new CausalTree<any, any, any>(stored, new MyReducer(), options)
|
52 | });
|
53 |
|
54 | // Connect to MongoDB
|
55 | MongoClient.connect('mongodb://my_mongo_db:27017' async (client) => {
|
56 |
|
57 | // Load and store data in MongoDB
|
58 | const causalTreeStore = new CausalTreeStoreMongoDB(client, 'myDB');
|
59 | await causalTreeStore.init();
|
60 |
|
61 | // Setup the channel manager
|
62 | const channelManager = new ChannelManagerImpl(
|
63 | causalTreeStore,
|
64 | causalTreeFactory
|
65 | );
|
66 |
|
67 | // Setup the device manager
|
68 | const deviceManager = new DeviceManagerImpl();
|
69 |
|
70 | // Create a causal tree server with the following services.
|
71 | const server = new CausalTreeServerSocketIO(
|
72 | serverDevice,
|
73 | socket,
|
74 | deviceManager,
|
75 | channelManager,
|
76 |
|
77 | // Allow any connection to be any user
|
78 | new NullDeviceAuthenticator(),
|
79 |
|
80 | // Allow any user to connect to any channel
|
81 | new NullChannelAuthorizer()
|
82 | );
|
83 |
|
84 | // Listen for connections on port 3000
|
85 | http.listen(3000);
|
86 | });
|
87 | }
|
88 |
|
89 | ```
|