Skip to content

Instantly share code, notes, and snippets.

@zackshapiro
Last active March 3, 2023 10:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zackshapiro/8c8b1967da5788ffed801ff4e9184dde to your computer and use it in GitHub Desktop.
Save zackshapiro/8c8b1967da5788ffed801ff4e9184dde to your computer and use it in GitHub Desktop.
Parse Separate Live Query Server Setup
// This is the index.js file for my Parse Live Query Server, running on a separate EC2 instance
var express = require('express');
var cors = require('cors')
var ParseServer = require('parse-server').ParseServer;
var app = express();
app.use(cors());
// We include the lines below so that we can hit `/` and it passes the Elastic Beanstalk Health Check
app.get('/', function(req, res) {
res.status(200).send('Make sure to star the parse-server repo on GitHub!');
});
var port = process.env.PORT || 1338;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
console.log('parse-server-example running on port ' + port + '.');
});
ParseServer.createLiveQueryServer(httpServer, {
appId: process.env.APP_ID, // same as index.js file below
masterKey: process.env.MASTER_KEY, // same as index.js file below
serverURL: process.env.SERVER_URL, // socket.myApp.com
javascriptKey: process.env.JAVASCRIPT_KEY,
redisURL: process.env.redisURL || "redis://:{password}@redis-123456.c11.us-east-1-3.ec2.cloud.redislabs.com:18091", // this is optional, include only if you're using Redis
websocketTimeout: 10 * 1000,
cacheTimeout: 60 * 600 * 1000,
verbose: process.env.VERBOSE_KEY || false,
});
// This is the index.js file for my main app
var express = require('express');
var cors = require('cors')
var ParseServer = require('parse-server').ParseServer;
var path = require('path');
var bodyParser = require('body-parser');
var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;
if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
// NOTE: `production` should be false if dev or local
// Always set to true when deploying to `master`.
var S3Adapter = require('parse-server').S3Adapter;
var s3Adapter = new S3Adapter(
process.env.S3BUCKET || 'media',
{ ... }
);
var api = new ParseServer({
databaseURI: databaseUri,
cloud: process.env.CLOUD_CODE_MAIN,
appId: process.env.APP_ID, // same as index.js file above
masterKey: process.env.MASTER_KEY, // same as index.js file above
serverURL: process.env.SERVER_URL, // live.myApp.com
javascriptKey: process.env.JAVASCRIPT_KEY,
clientKey: process.env.CLIENT_KEY,
filesAdapter: s3Adapter,
verbose: process.env.VERBOSE_KEY || false,
allowClientClassCreation: process.env.CLIENT_CREATION || false,
liveQuery: {
classNames: ['messages', '_User'],
redisURL: process.env.redisURL || "redis://:{password}@redis-123456.c11.us-east-1-3.ec2.cloud.redislabs.com:18091" // this is optional, include only if you're using Redis
},
databaseOptions: { poolSize: 500 },
maxUploadSize: "5mb"
});
var app = express();
app.use(bodyParser.json({limit: '5mb'}) );
app.use(bodyParser.urlencoded({limit: '5mb', extended: true}));
app.use(cors());
app.use('/public', express.static(path.join(__dirname, '/public')));
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);
app.get('/', function(req, res) {
res.status(200).send('Make sure to star the parse-server repo on GitHub!');
});
var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
console.log('parse-server-example running on port ' + port + '.');
});
@stoodkev
Copy link

Hi! Im running the exact same code with my redis instance locally, and it seems like both sides connect to redis without issue.
However, when I create an object with a class referenced for LiveQueries, nothing is written in redis, and hence the lq server doesn't receive anything.
Any idea what could cause this behavior?

@rsmets
Copy link

rsmets commented Jan 27, 2020

@stoodkev same here actually! LQ is not getting any create event over redis. Curious if you were able to get things working?

@stoodkev
Copy link

Nop, Im stuck there as well. I ll probably have to stop using LQ to be able to deploy in an autoscaling group in AWS

@rsmets
Copy link

rsmets commented Jan 28, 2020

So I was actually able to get my setup working. My issue was just an over site in the new env main parse server config. The server url env var was actually pointing to the env I took the config from so even though I was hitting the new env which has the LQ redis adapter configured (and stand alone LQ server on the other end) internally the new env was talking to the old all-in-one setup thus the db hooks code path in the new env were never being hit thus no events produced to redis.

I am not exactly sure I understand your auto scaling constraint but getting my setup to work with AWS ECS did take some work. Mainly around realizing the need to enable “stickiness” on the ALB listeners to allow for the websocket connections to work. Dispute all the chatter about nginx config on here no need for nginx with the proper ALB config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment