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. But it also has super feature: Ability to Syncornize to remote database (or across multiple devices).

I had to setup environment for syncronization when I prepared presentation for local event, and I used AWS EC2 Micro instance with standard Ubuntu x64, but it also works on local environment and using different type of unix.
You can download Couchbase Server, Sync Gateway and Couchbase Lite framework from offical site.

Download and 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

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

Prepare Sync Gateway Config

{
"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.

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:

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

or using configuration:

"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

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

You can also run with nohup:

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

it leaves running server in background even if you close ssh session.

Check Access

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

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

Setup is pretty stratforward: video. We can configure different buckets for different puposes, 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

{
"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

# 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/

OpenShift: Ruby on Rails Deployment Steps

This is a simple list of deployment steps to setup Ruby on Rails app on OpenShift.

Well, I assume we have OpenShift account and rhc tool installed and logged in. We also have a git repo with a brand new Ruby on Rails app.

What’s next:

  1. Create MyApp
rhc app-create -a MyApp --scale -t ruby-1.9 --no-git
  1. Setup Database. I used postgresql, but mysql-5.1/5.5 and mongoDB-2.4 are also available.
rhc cartridge add postgresql-9.2 -a MyApp
  1. Prepare database.yml:
production:
adapter: postgresql
database: <%=ENV['OPENSHIFT_APP_NAME']%>
username: <%=ENV['OPENSHIFT_POSTGRESQL_DB_USERNAME']%>
password: <%=ENV['OPENSHIFT_POSTGRESQL_DB_PASSWORD']%>
host: <%=ENV['OPENSHIFT_POSTGRESQL_DB_HOST']%>
port: <%=ENV['OPENSHIFT_POSTGRESQL_DB_PORT']%>
min_messages: ERROR
reconnect: false
pool: 5
timeout: 5000
encoding: utf8
  1. Check everything working:
bundle install
rspec
rails s
  1. Add the OpenShift repository as one of your remote repositories
    • Show you all the information about your application (Remote URL)
rhc app-show MyApp
  • Add Git Remote
git remote add openshift <REMOTE_URL>
  • Take whatever OpenShift is adding by merging (saving our code)
git merge openshift/master -s recursive -X ours
  • Let’s push it:
git push openshift master

It usually takes a few minutes (not seconds). And should be finished like this:

...
remote: Starting Ruby cartridge
remote: -------------------------
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success
  1. Connect SSH:
rhc ssh MyApp
  1. Navigate to App folder:
cd app-root/repo
  1. Migrate Database:
bundle exec rake db:migrate RAILS_ENV="production"
  1. Seed
bundle exec rake db:seed RAILS_ENV="production"

Now, it is time to open it in your browser. Happy Deployment!

Grabbing Multi Monitor Screenshot using Qt

You all (who works with Qt Framework) know an example of how to grab the Screenshot using Qt.

But, I’ve found that this example just grubs screenshot only from primary screen in Multi Monitor Environment.

So I had method like this:

QPixmap ScreenshotMaker::makeScreenshot() {
return QPixmap::grabWindow(QApplication::desktop()->winId());
}

I was thinking of writing somthing in WinAPI, like this :
Creation of Multi-monitor Screenshots Using WinAPI, and after that, searching for Linux and Mac platform dependent solutions…

But, after some researching and experiments with Qt, I’ve found interesting, but easy solution:

QPixmap ScreenshotMaker::makeScreenshot() {
QDesktopWidget * desktop = QApplication::desktop();
// Fix for multi screen
return QPixmap::grabWindow(desktop->winId(),
desktop->x(),
desktop->y(),
desktop->width(),
desktop->height());
}

That gives very good results. It takes screenshot of whole desktop area.

Hopefully it will save somebody’s time.