Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add usage example #6

Open
curran opened this issue Jun 11, 2017 · 2 comments
Open

Add usage example #6

curran opened this issue Jun 11, 2017 · 2 comments

Comments

@curran
Copy link
Contributor

curran commented Jun 11, 2017

No description provided.

@curran
Copy link
Contributor Author

curran commented Jun 27, 2020

@curran
Copy link
Contributor Author

curran commented Jun 27, 2020

Here's a complete starter.

import redis from 'redis';
import redisPubSub from 'sharedb-redis-pubsub';

import ShareDB from 'sharedb'; 
import ShareDBMingoMemory from 'sharedb-mingo-memory';
import ShareDBMongo from 'sharedb-mongo';

// Singletons.
let shareDB;
let connection;

// Environment variables.
const mongoURI = process.env.MONGO_URI; 
const redisHost = process.env.REDIS_HOST;

export const getShareDB = () => {
  if (!shareDB) {
    const options = {
      db: mongoURI ? new ShareDBMongo(mongoURI) : new ShareDBMingoMemory(),
    };
    if (process.env.VIZHUB_REDIS_HOST) {
      options.pubsub = redisPubSub({
        client: redis.createClient({ host: redisHost }),
      });
    }
    shareDB = ShareDB(options);
  }
  return shareDB;
};

export const getConnection = () => {
  if (!connection) {
    connection = getShareDB().connect();
  }
  return connection;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant