Connect Couchbase Gateway to Couchbase server


#1

Hi,

I’m brand new to couches and I’m trying to learn how to use the Couchbase mobile with a Gateway and server I have setup on a remote Windows 2012 VM. I walked through many of the online examples but I’m not seeing exactly how to connect all the pieces. So far I’ve managed to successfully connect my HelloWorld Android app to the gateway running the walrus in memory db. I’ve also managed to get basic Authenthication authorization working. I’ve just attempted to copy my config file for the Gateway to make a clone which connects to the Server (running on the same VM) using the default bucket:

{
  "log": ["CRUD+", "REST+", "Changes+", "Attach+"],
  "databases": {
    "default": {
      "server":"http://10.0.0.4:8091",
      "sync": ` function(doc) { channel (doc.channels); } `,
      "users": { "GUEST": { "disabled": true, "admin_channels": ["*"] } }
    }
  }
}

The gateway launches without issue but when I run my client I get errors:

07-31 12:47:09.323  28478-28515/com.cliftoncraig.couchbase.hellocouchbasegateway D/Sync﹕ com.couchbase.lite.support.RemoteRequestRetry$1@42007c70: RemoteRequestRetry failed, non-transient error.  NOT retrying. url: http://myserver.net:4984/default/_session
07-31 12:47:09.325  28478-28524/com.cliftoncraig.couchbase.hellocouchbasegateway E/RemoteRequest﹕ Got error status: 401 for http://myserver.net:4984/default/_session.  Reason: Unauthorized

I am using the very same authorization credentials from my original config json as a matter of fact it is the same config file where I only changed the database name and the server URL. If I use the same app with the in memory config (below) it appears to sync just fine.

{
  "log": ["CRUD+", "REST+", "Changes+", "Attach+"],
  "databases": {
    "couchbaseevents": {
      "server":"walrus:data",
      "sync": ` function(doc) { channel (doc.channels); } `,
      "users": { "GUEST": { "disabled": true, "admin_channels": ["*"] } }
    }
  }
}

Here’s the relevant part of my Android client logic:

    private void initialize() {
        // Create the document
        this.database = ((Application) getApplication()).getDatabase();
        try { startReplications(database); }
        catch (CouchbaseLiteException e) { throw new RuntimeException("Could not start Couchbase replication.",e); }
        String documentId = createDocument(database);
        // retrieve the document from the database
        Document retrievedDocument = database.getDocument(documentId);
        // display the retrieved document
        AppLog.log("retrievedDocument=" + String.valueOf(retrievedDocument.getProperties()));
    /* Get and output the contents */
//        outputContents(database, documentId);
    /* Update the document and add an attachment */
        updateDoc(database, documentId);
        AppLog.log("After update retrievedDocument=" + String.valueOf(retrievedDocument.getProperties()));
        // Add an attachment
        addAttachment(database, documentId);
    /* Get and output the contents with the attachment */
        outputContentsWithAttachment(database, documentId);
        documentId = retrievedDocument.getId();
        try { retrievedDocument.delete(); }
        catch (CouchbaseLiteException e) { throw new RuntimeException("Error deleting document " + documentId,e); }
        AppLog.log("Document deleted.");
        retrievedDocument = database.getDocument(documentId);
        AppLog.log("Document retrieved after delete: " + retrievedDocument.getProperties());

        List<Map> events = createTestEvents();
        for(Map eachEvent : events) {
            saveData(database, eachEvent);
        }
    }

It calls startReplication below:

    private void startReplications(Database database) throws CouchbaseLiteException {
        final URL url;
//        try { url = new URL("http://craigwserver.cloudapp.net:4984/couchbaseevents"); }
        try { url = new URL("http://myserver.net:4984/default"); }
        catch (MalformedURLException e) { throw new RuntimeException("Invalid URL",e);}
        Replication pull = database.createPullReplication(url);
        Replication push = database.createPushReplication(url);
        Authenticator authenticator = AuthenticatorFactory.createBasicAuthenticator("couchbase_user", "mobile");
        pull.setAuthenticator(authenticator);
        push.setAuthenticator(authenticator);
        pull.setContinuous(true);
        push.setContinuous(true);
        pull.start();
        push.start();
    }

#3

Are you sure the user with name couchbase_user and password mobile exists?
Note that when running SG in walrus mode, you have to create the user through the admin rest api.

In production, you could expose that endpoint to client applications to display a sign up screen (this tutorial explains how to do it).

James