Couchbase Sync Gateway Setup in 5 minutes to use with Couchbase Lite

Couchbase Lite is a nice schemaless (NoSQL) database for mobile (iOS, Android,…).
It is good for local storage and easy to use in mobile app. And it also has super feature: Ability to Syncornize to remote database (or across multiple devices).

Preparing demo for local event I configured environment for synchronization using AWS EC2 Micro instance with standard Ubuntu x64. Alternatively it could be setup locally on variety of different Unix systems.

Couchbase Server, Sync Gateway and Couchbase Lite framework can be downloaded from official site.

Download and Install Sync Gateway

1
2
wget http://packages.couchbase.com/releases/couchbase-sync-gateway/1.0.0/couchbase-sync-gateway-community_1.0.0_x86_64.deb
sudo dpkg -i couchbase-sync-gateway-community_1.0.0_x86_64.deb

By default it will be installed to /opt/couchbase-sync-gateway/bin/sync_gateway

Prepare Sync Gateway Config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"log": ["CRUD", "CRUD+", "HTTP", "HTTP+", "Access", "Cache", "Changes", "Changes+"],
"interface":":4984",
"adminInterface":":14985",
"facebook":{
"register":true
},

"databases":{
"gw":{
"server":"walrus:data",
"bucket":"default",
"sync": ` function(doc, oldDoc) { channel("all_docs"); } `
}

}

}

Or download my example from here. It uses Walrus as dataabase, enabled facebook authentication, enabled verbose logging, and used simplest sync function which will sync everything (for complex cases it needs to be improved).

Walrus is a simple, limited, in-memory database that you can use in place of Couchbase Server for unit testing during development. But it needs data folder on disk to store temprary files.

Guest Access

You might want to enable guest access when you try Couchbase Lite Sync for the first time, it can be done using admin API:

1
curl -X PUT localhost:4985/gw/_user/GUEST --data '{"disabled":false, "admin_channels":[“*”]}'

or using configuration:

1
2
3
4
5
6
7
8
9
10
"databases":{
"gw":{
"users": {
"GUEST": {"disabled": false, "admin_channels": ["*"] }
},
"server":"http://localhost:8091",
"bucket":"default",
"sync": ` function(doc, oldDoc) { channel("all_docs");} `
}
}

You can simply add GUEST user enabled with access to all channels.

Run Sync Gateway

1
/opt/couchbase-sync-gateway/bin/sync_gateway syncgateway_walrus_config.json

You can also run with nohup:

1
nohup /opt/couchbase-sync-gateway/bin/sync_gateway syncgateway_walrus_config.json

It keeps the server running in the background even after ssh session has been closed.

Check Access

1
curl <YOUR_SERVER_URL>:4984/gw/

Now you can use this url for PULL/PUSH replication (syncronization).

But this environment does not give you all advantages, that’s why I always install real server (even if my environment is far from required)

Download Server

1
2
wget http://packages.couchbase.com/releases/2.2.0/couchbase-server-community_2.2.0_x86_64.deb
sudo dpkg -i couchbase-server-community_2.2.0_x86_64.deb

Setup Database

1
http://<YOUR_SERVER_URL>:8091/index.html

Setup is pretty straightforward: video. We can configure different buckets for different purposes, but I use default in configs.

And we should change configuration server to http://localhost:8091 in sync gateway config. (sync_gateway restart required)

Prepare Sync Gateway Config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"log": ["CRUD", "CRUD+", "HTTP", "HTTP+", "Access", "Cache", "Changes", "Changes+"],
"interface":":4984",
"adminInterface":":14985",
"facebook":{
"register":true
},

"databases":{
"gw":{
"server":"http://localhost:8091",
"bucket":"default",
"sync": ` function(doc, oldDoc) { channel("all_docs"); } `
}

}

}

Or download my example from here, or version with GUEST enabled here.

Summary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# download & install sync gateway 
wget http://packages.couchbase.com/releases/couchbase-sync-gateway/1.0.0/couchbase-sync-gateway-community_1.0.0_x86_64.deb
sudo dpkg -i couchbase-sync-gateway-community_1.0.0_x86_64.deb

# download & install couchbase server
wget http://packages.couchbase.com/releases/2.2.0/couchbase-server-community_2.2.0_x86_64.deb
sudo dpkg -i couchbase-server-community_2.2.0_x86_64.deb

# setup database
http://<YOUR_SERVER_URL>:8091/index.html

# download config (default bucket)
wget http://goo.gl/i4sKcS

# run sync gateway (use nohup if necessary)
/opt/couchbase-sync-gateway/bin/sync_gateway syncgateway_config.json

# check access
curl <YOUR_SERVER_URL>:4984/gw/