3

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?

2
  • yes, there is nothing bad on it. Commented Oct 30, 2015 at 11:35
  • 2
    This has a very good answer to your question: stackoverflow.com/questions/19477821/… Commented Oct 30, 2015 at 12:37

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.