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?

Charlie
  • 4,197
  • 5
  • 42
  • 59

1 Answers1

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.

Rodmentou
  • 1,610
  • 3
  • 21
  • 39