-
Regina allows to run MongoDB
'insert'
,'find'
,'update'
,'delete'
,'count'
, and'aggregate'
methods directly from the client side (such as firebase). -
Regina can track
tags
based events and send back messages containing theresult
of the requests and theircontext
to client's sockets subscribed to these tags. -
Regina uses Socket.IO for client-server communication and event tracking.
npm install -g regina
Run with the default settings (
db='localhost:27017/reginadb'
andport=3009
) :
mongod
regina
- open your browser at
localhost:3009
and check that you are on the regina home page.
Run with custom settings :
mongod --port 5000
regina 'localhost:5000/mydb' 6000
- open your browser at
localhost:6000
check that you are on the regina home page.
Import socket.io client and follow these instructions :
- create a socket instance with the regina server address :
var socket = io('http://localhost:3009/');
- send requests to the regina server using one of these type of requests :
socket.emit('insert', collection, docs, options, meta, ack);
socket.emit('find', collection, query, options, meta, ack);
socket.emit('count', collection, query, options, meta, ack);
socket.emit('update', collection, query, update, options, meta, ack);
socket.emit('remove', collection, query, options, meta, ack);
socket.emit('aggregate', collection, pipeline, options, meta, ack);
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
<script>
//create a socket instance with the regina server address
var socket = io('http://localhost:3009/');
//be aware of the misuse of regina methods
socket.on('regina_noack_callback_error', (msg) => { console.log(msg); })
//follow the 'new-msg' tag
socket.on('new-msg', (res, ctx) => { console.log(res, ctx); });
//send an insert request to the regina server with the 'new-msg' tag
socket.emit('insert' //CRUD operation
, 'messages' //collection
//query|doc|pipeline
, { msg: "Hello Regina", sender: "Paris MongoDB User Group" }
, {} //mongo options
, { "tags": [{ "val" : "new-msg" }] } //meta (tags)
, (err, res, ctx) => { console.log(err, res, ctx); } //ack (callback)
);
</script>
You can use any tag you want except socket.io reserved events. In the
meta
parameter, simply add an object containing thetags
key and an array of objects each containing theval
key.
{"tags":[{"val":"find-users"}, {"val":"@users-coll"}, {"val":"#users"}]}
You can also specify the
kind
(scope) for each tag :
{"tags":[{"val":"find-users","kind":"emit"}, {"val":"#users","kind":"broadcast"}]}
There are 3 kinds of scopes:
emit
: sends a message only to the client that sent the request to the server.broadcast
: sends a message to all connected clients except the client that sent the request to the server.io
: sends a message to all connected clients including the client that sent the request to the server. By default the scope isio
.