Rather than use the likes of Redis, or even LokiJS (which looks great), can I just create a large javascript collection in memory as my app runs, and query that instead?
-
yes, there is nothing bad on it.webduvet– webduvet2015-10-30 11:35:56 +00:00Commented Oct 30, 2015 at 11:35
-
2This has a very good answer to your question: stackoverflow.com/questions/19477821/…Wylie Kulik– Wylie Kulik2015-10-30 12:37:28 +00:00Commented Oct 30, 2015 at 12:37
Add a comment
|
1 Answer
I have an app with this exact pattern using socket.io.
Bellow, I have translated my socket.io code to use HTTP Requests.
On the server, you can do something like that:
var players = []; //This will be used to store in-memory players.
Them,
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); //Need this to populate req.body on requests.
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
next(); //Need this to accept incoming requests.
});
app.get('/players', function (req, res) {
res.json(players);
});
app.post('/players', function (req, res) {
//validation
players.push(req.body);
});
[...]
Look at this example: http://agar.io/
This is a very popular game. Now, why would they store your position, score or name, while playing, on database? This would be very costly.