A newer version of this documentation is available.

View Latest
March 23, 2025
+ 12

Assigning Sync Gateway channels

Related Topics:    access()   |   channel()   |   expiry()   |   requireAccess()   |   requireAdmin()   |   requireRole()   |   requireUser()   |   role()   |   throw()

Function Call

channel(channelname)

Purpose

Use the channel() function to route the document to the named channel(s).

Arguments

Argument Description

channels

Must be a string identifying a channel name, or an array of strings to specify multiple channel names (for example: (['channel1', 'channel2']); the function is applied to each element in the array.

If the value resolves to null the function result is a no-op.

Context

The channel function can be called zero or more times from the sync function, for any document.

Channels don’t have to be predefined.
A channel implicitly comes into existence when a document is routed to it.

Routing changes have no effect until the document is actually saved in the database, so if the sync function first calls channel() or access(), but then rejects the update, the channel and access changes will not occur.

As a convenience, it is legal to call channel with a null or undefined argument; it simply does nothing.
This allows you to do something like channel(doc.channels) without having to first check whether doc.channels exists.

Use

Example 1. channel(channelname)

This example routes all "published" documents to the "public" channel:

javascript
function (doc, oldDoc, meta) { if (doc.published) { channel("public"); } }