Meteor 1.3+ - Connect to Redis on the Server
While doing research on backend connectors for the new Apollo Stack by the Meteor team I wanted to easily connect to a redis server from my own Meteor server. The following is a writeup of the steps to set this up. As always the full source code for this tutorial is available on my GitHub.
We can download redis onto your computer from their download page. Mac users that have homebrew can also easily install it via
brew install redis. The rest of this tutorial will assume you are running redis on a Mac.
Once redis is installed you can start it by just typing
redis-server which should give you the following prompt:
Notice that it is using the default port of
6379. If you want to change this port from the default you will also need to change the way your redis package connects to the server. You can also play around with the server by typing
redis-cli and manually enter in commands in the cli.
We also need to install the following packages to get redis setup. The most notable is node_redis which allows us to communicate with the redis server we installed in the previous step.
meteor npm install --save redis meteor npm install --save hiredis
Server Side Redis
Setting up redis on the server is fairly simple, as you just need the following code:
We require the redis package, create a client from it, and wrap the
set functions so that they can easily be used with
Meteor.methods. We then export the client so it can be used in any other modules by just calling
require("redis.js"). If you want to change the default port you can pass in additional options to
createClient. A full overview of the api is available on the node_redis GitHub page.
The meteor methods are very basic for testing purposes. We just have a simple
getRedis method that use our wrapped redis functions. This allows us to test the redis functionality from the client.
Client Side Code
The client side code is composed of a few input fields and buttons that are hooked up to
Meteor.call code. This allows us to test our simple
set will add the key/value pair to the redis server. You can then retrieve the value by clicking
get which will populate the
Get Value label.
That’s it for this short tutorial with the bulk of the content being installing and setting up redis on the server. As you can see it’s very straightforward to add data stores other than MongoDB to your Meteor application, all thanks to the new npm support provided in Meteor 1.3+. As always the full source code for this tutorial is available on my GitHub.
Curtis Larson is a LA based freelance software developer. Follow him on Twitter or send him an email if you're looking for a talented developer.