Welcome to a growing collection of persistence adapters for PubNub using Node.JS. In this sample, we present a proof-of-concept for global, multi data center MongoDB replication data using PubNub.
Note: this is not production-ready! It is supposed to provoke inspiration and conversation while we work, test and get the bugs out. Thanks so much for checking it out!
npm install mongodb -g
For this example, try out the following steps when configuring a test MongoDB instance:
$ ./bin/mongod --replSet test --dbpath ./data
> rs.initiate({_id: "test", members: [{_id: 0, host: "127.0.0.1:27017"}]})
MongoDB uses Replication Sets as a primary grouping mechanism for replication. Within each replica set, one node is configured as a primary. If the cluster detects a primary node failure, a secondary node will be elected to become a new master. The oplog is replicated to each node and contains itemized updates which are applied to each node’s state in-order to determine the current state of the collection. The oplog also has a fixed size which determines the number of events stored in its history.
For more information, check out:
To start a MongoDB replication client, use this command:
This starts up the replication process. You should see something like this:
Now, perform a few operations on your collection, for example:
{"email":"dude@dude.com"}
{"email":"dude@dude.com"}
(again){"email":"dude@dude.com"}
(again){"email":"dude@dude.com"}
If you’re using the mongodb nodejs client, you’d do something like this:
Whoa, that’s pretty awesome! You should see something like:
Pretty awesome! That means that the replication client saw the update, and sent it out on the “mongochan” channel.
What happened? The replication client connected to your MongoDB instance, started listening to the oplog, and sent oplog change events to the PubNub channel “mongochan”. The cool thing is that even though it was one operation, the “delete” operation removed three records under the hood, so there are three entries in the oplog.
To start a MongoDB replication listener, use this command:
This starts a listener on the given pubnub channel. You can modify the listener to do whatever you want with the data it receives! (This one just logs to console)
As you perform updates, you should see messages like this:
We hope you enjoyed this example of replicating MongoDB data using PubNub and Node.JS. In the future, we’ll implement and review more MongoDB functionality, and start getting this sample code a bit more production-ready. If you run into any issues or have any suggestions for making this more awesome, please drop us a line!
There are common underlying technologies for a dating app, and in this post, we’ll talk about the major technologies and designs...
Michael Carroll
How to use geohashing, JavaScript, Google Maps API, and BART API to build a real-time public transit schedule app.
Michael Carroll
How to track and stream real-time vehicle location on a live-updating map using EON, JavaScript, and the Mapbox API.
Michael Carroll