From 15fc4cf4f2256d40478b0745851a0eeca99e6494 Mon Sep 17 00:00:00 2001 From: haad Date: Mon, 22 Feb 2016 15:26:02 +0200 Subject: [PATCH] Replace redis with socket.io connection to orbit-server --- examples/keyvalue.js | 2 +- package.json | 2 +- src/OrbitClient.js | 2 +- src/PubSub.js | 31 ++++++++++--------------------- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/examples/keyvalue.js b/examples/keyvalue.js index fd600bf..8739513 100644 --- a/examples/keyvalue.js +++ b/examples/keyvalue.js @@ -7,7 +7,7 @@ const Timer = require('./Timer'); // Redis const host = 'localhost'; -const port = 6379; +const port = 3333; const username = 'LambOfGod'; const password = ''; diff --git a/package.json b/package.json index cecb593..24aa8b2 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "asyncawait": "^1.0.1", "lodash": "^4.3.0", "orbit-common": "^0.1.0", - "redis": "^2.4.2" + "socket.io-client": "^1.4.5" }, "devDependencies": { "mocha": "^2.3.4" diff --git a/src/OrbitClient.js b/src/OrbitClient.js index 9517b7d..c34649f 100644 --- a/src/OrbitClient.js +++ b/src/OrbitClient.js @@ -11,7 +11,7 @@ const HashCacheOps = require('./HashCacheOps'); const ItemTypes = require('./ItemTypes'); const MetaInfo = require('./MetaInfo'); const Post = require('./Post'); -const PubSub = require('./Pubsub'); +const PubSub = require('./PubSub'); const List = require('./list/OrbitList'); const DataStore = require('./DataStore'); diff --git a/src/PubSub.js b/src/PubSub.js index b122f34..fb12044 100644 --- a/src/PubSub.js +++ b/src/PubSub.js @@ -1,42 +1,31 @@ 'use strict'; -const redis = require("redis"); -const List = require('./list/OrbitList'); +const io = require('socket.io-client'); +const List = require('./list/OrbitList'); class Pubsub { constructor(ipfs, host, port, username, password) { this.ipfs = ipfs; this._subscriptions = {}; - this.client1 = redis.createClient({ host: host, port: port }); - this.client2 = redis.createClient({ host: host, port: port }); - this.client1.on("message", this._handleMessage.bind(this)); - // this.client1.on('connect', () => console.log('redis connected')); - // this.client1.on("subscribe", (channel, count) => console.log(`subscribed to ${channel}`)); + this._socket = io(`http://${host}:${port}`); + this._socket.on('connect', (socket) => console.log('Connected to', `http://${host}:${port}`)); + this._socket.on('message', this._handleMessage.bind(this)); } subscribe(hash, password, callback) { - if(!this._subscriptions[hash] || this._subscriptions[hash].password !== password) { - this._subscriptions[hash] = { - password: password, - head: null, - callback: callback - }; - this.client1.subscribe(hash); + if(!this._subscriptions[hash]) { + this._subscriptions[hash] = { head: null, callback: callback }; + this._socket.emit('subscribe', { channel: hash }); } } unsubscribe(hash) { + this._socket.emit('unsubscribe', { channel: hash }); delete this._subscriptions[hash]; - this.client1.unsubscribe(); - this.client2.unsubscribe(); } publish(hash, message) { - this.client2.publish(hash, message); - } - - latest(hash) { - return { head: this._subscriptions[hash] ? this._subscriptions[hash].head : null }; + this._socket.send({ channel: hash, message: message }); } _handleMessage(hash, message) {