Skip to content

Commit ea6c385

Browse files
committed
Adds REMOTE_SB env to server
which connects to the specified remote redux-scuttlebutt server
1 parent 6d81735 commit ea6c385

File tree

3 files changed

+59
-26
lines changed

3 files changed

+59
-26
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ The store enhancer takes an options object, including the key
6969
```js
7070
scuttlebutt({
7171
// uri of a scuttlebutt peer or server
72-
uri: 'http://localhost:3000',
72+
uri: `${window.location.protocol}//${window.location.host}`,
7373

7474
// options for primus.io <https://github.com/primus/primus#getting-started>
7575
primusOptions: {},

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export { META_SOURCE, META_TIMESTAMP, REWIND_ACTION } from './constants'
66

77
// Applies default options.
88
const defaultOptions = {
9-
uri: 'http://localhost:3000',
9+
uri: (typeof window === 'object' && `${window.location.protocol}//${window.location.host}`),
1010
primusOptions: {},
1111
primus: (typeof window === 'object' && window.Primus),
1212
dispatcherOptions: {},

src/server.js

+57-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fs from 'fs'
22

33
const INFILE = process.env['INFILE'],
4-
OUTFILE = process.env['OUTFILE']
4+
OUTFILE = process.env['OUTFILE'],
5+
REMOTE_SB = process.env['REMOTE_SB']
56

67
export default function scuttlebuttServer(server) {
78
const primusServer = new (require('primus'))(server, {}),
@@ -55,38 +56,70 @@ export default function scuttlebuttServer(server) {
5556

5657
}, 10000) // max 6/minute
5758

59+
// connect dispatcher to redux
5860
connectRedux(gossip)
5961

60-
if (INFILE || OUTFILE) {
61-
if (INFILE) {
62-
const gossipWriteSteam = gossip.createWriteStream()
63-
fs.createReadStream(INFILE).pipe(gossipWriteSteam)
62+
// read actions from file
63+
if (INFILE) {
64+
const gossipWriteSteam = gossip.createWriteStream()
65+
fs.createReadStream(INFILE).pipe(gossipWriteSteam)
6466

65-
console.log('📼 Reading from ' + INFILE)
66-
}
67+
console.log('📼 Reading from ' + INFILE)
68+
}
6769

68-
if (OUTFILE) {
69-
const gossipReadSteam = gossip.createReadStream()
70+
// stream actions to file -- this will include all actions in INFILE
71+
if (OUTFILE) {
72+
const gossipReadSteam = gossip.createReadStream()
7073

71-
// For some reason, we're not getting any 'sync' events from Dispatcher,
72-
// so we'll listen for it in the datastream and write to disk after it
73-
// <https://github.com/dominictarr/scuttlebutt#persistence>
74+
// For some reason, we're not getting any 'sync' events from Dispatcher,
75+
// so we'll listen for it in the datastream and write to disk after it
76+
// <https://github.com/dominictarr/scuttlebutt#persistence>
7477

75-
gossipReadSteam.on('data', (data) => {
76-
if (data === '"SYNC"\n') {
77-
console.log('📼 Writing to ' + OUTFILE)
78-
gossipReadSteam.pipe(fs.createWriteStream(OUTFILE))
79-
}
80-
})
81-
82-
// this doesn't fire.
83-
gossip.on('sync', function () {
84-
console.log('📼 [NATURAL SYNC] Writing to ' + OUTFILE)
78+
gossipReadSteam.on('data', (data) => {
79+
if (data === '"SYNC"\n') {
80+
console.log('📼 Writing to ' + OUTFILE)
8581
gossipReadSteam.pipe(fs.createWriteStream(OUTFILE))
86-
})
82+
}
83+
})
84+
85+
// this doesn't fire.
86+
gossip.on('sync', function () {
87+
console.log('📼 [NATURAL SYNC] Writing to ' + OUTFILE)
88+
gossipReadSteam.pipe(fs.createWriteStream(OUTFILE))
89+
})
90+
91+
console.log('📼 Ready to write to ' + OUTFILE)
92+
}
93+
94+
// connect to remote redux-scuttlebutt instance
95+
if (REMOTE_SB) {
96+
var remoteStream = gossip.createStream(),
97+
remoteClient = new primusServer.Socket(REMOTE_SB)
8798

88-
console.log('📼 Ready to write to ' + OUTFILE)
99+
console.log('💡 connecting to remote '+ REMOTE_SB)
100+
101+
remoteClient.pipe(remoteStream).pipe(remoteClient)
102+
103+
statistics['REMOTE_SB'] = {
104+
recv: 0, sent: 0, s: 'remote'
89105
}
106+
107+
remoteClient.on('data', function recv(data) {
108+
// console.log('[io]', 'REMOTE_SB', '<-', data);
109+
statistics['REMOTE_SB'].recv++
110+
statisticsDirty = true
111+
});
112+
113+
remoteStream.on('data', (data) => {
114+
// console.log('[io]', 'REMOTE_SB' || 'origin', '->', data);
115+
statistics['REMOTE_SB'].sent++
116+
statisticsDirty = true
117+
})
118+
119+
remoteStream.on('error', (error) => {
120+
console.log('[io]', 'REMOTE_SB', 'ERROR:', error);
121+
remoteClient.end('Disconnecting due to error', { reconnect: true })
122+
})
90123
}
91124

92125
primusServer.on('connection', (spark) => {

0 commit comments

Comments
 (0)