Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Exploring the Public and User Channels in Couchbase Sync Gateway

DZone's Guide to

Exploring the Public and User Channels in Couchbase Sync Gateway

The Sync Function is one of the fundamental components of Sync Gateway. Its main responsibility is to define which user has access to which documents. In this screencast, you’ll learn how to use the public channel to synchronize documents to users that haven’t logged into your app yet.

· Database Zone
Free Resource

Traditional relational databases weren’t designed for today’s customers. Learn about the world’s first NoSQL Engagement Database purpose-built for the new era of customer experience.

The Sync Function is one of the fundamental components of Sync Gateway. Its main responsibility is to define which user has access to which documents. In this screencast, you’ll learn how to use the public channel (also known as the ! channel) to synchronize documents to users that haven’t logged into your app yet. This feature is particularly useful when designing the onboarding journey with dynamic data that can be changed on the backend and pulled automatically when the user starts the app for the first time. We’ll consider a note taking application and explore the following:

  1. Use the public channel to insert the Getting Started note, you can think of it as the tutorial note when the user starts the app for the first time.
  2. Map the note created by a registered user to a channel that is only accessible by that user.
  3. Enforce write permissions with the requireUser method.
  4. Test the Sync Function with curl commands.

Find the Code for This Lesson

You can find the Sync Gateway configuration file below:

{
  "log": ["CRUD"],
  "databases": {
    "notes": {
      "server": "walrus:",
      "users": {
        "GUEST": {"disabled": false},
        "william": {"password": "pass"},
        "admin": {"password": "pass"}
      },
      "sync": `
        function sync(doc, oldDoc) {
          switch(doc.security) {
            case "public":
              requireUser("admin");
              channel("!");
              break;
            default:
              requireUser(doc.user_name);
              channel(doc._id);
              access(doc.user_name, doc._id);
          }
        }
      `
    }
  }
}

And the curl commands to test the Sync Function:

  1. Persist a Public Note via the Admin API
    curl -X POST http://localhost:4985/notes/ \
        -H "Content-Type: application/json" \
        -d '{"security": "public", "title": "Getting Started"}'
  2. User Authentication
    curl -vX POST http://localhost:4984/notes/_session \
        -H "Content-Type: application/json" \
        -d '{"name": "william", "password": "pass"}'
  3. Persist a User Note
    curl -X POST http://localhost:4984/notes/ \
        -H "Content-Type: application/json" \
        -H "Cookie: SyncGatewaySession=PASTE_COOKIE_VALUE" \
        -d '{"user_name": "william", "title": "Overview of the user journey"}'

Learn how the world’s first NoSQL Engagement Database delivers unparalleled performance at any scale for customer experience innovation that never ends.

Topics:
couchbase ,sync gateway ,database

Published at DZone with permission of James Nocentini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}