Replication channels and sync function

69 views
Skip to first unread message

Frederic Yesid Peña Sánchez

unread,
Mar 25, 2014, 11:39:02 AM3/25/14
to mobile-c...@googlegroups.com
Hi.

We have currently 2 channels enabled for PULL on iOS application: "pmsync_todos" & "pmsync_USER" where USER is the currently logged in user.

We need to deploy certain documents for all users on "pmsync_todos" channel, and other documents on a per user basis on each "pmsync_USER" channel.

Trying to understand the sync function, how could i authorize "pmsync_todosl" channel for all users?

This is my sync function:

function(doc) {

        var coincide;

        if(doc.pmsync_channels){

                channel(doc.pmsync_channels);

                for(var i = 0; i < doc.pmsync_channels.length; i++){

                        if(doc.pmsync_usuarios)

                                for(var j = 0; j < doc.pmsync_usuarios.lenght; j++)

                                        access(doc.pmsync_channels[i],doc.pmsync_usuarios[j]);

                        if(doc.pmsync_usuario)

                                access(doc.pmsync_channels[i], doc.pmsync_usuario);

                }

        }

}


My current approach for enabling access to all users for channel is to create a document for each user, using the "pmsync_todos" channel.

{
	"pmsync_channels”:[“pmsync-todos"],
	"pmsync_usuario”:”pmsync-rafael"
}

Should this document enable the channel for my users? or how should be it?

Jens Alfke

unread,
Mar 25, 2014, 1:55:15 PM3/25/14
to mobile-c...@googlegroups.com

On Mar 25, 2014, at 8:39 AM, Frederic Yesid Peña Sánchez <freder...@gmail.com> wrote:

> We need to deploy certain documents for all users on "pmsync_todos" channel, and other documents on a per user basis on each "pmsync_USER" channel.
> Trying to understand the sync function, how could i authorize "pmsync_todosl" channel for all users?

One approach is to use server-side code to add it to the user’s admin_channels when you register the account.

> for(var i = 0; i < doc.pmsync_channels.length; i++){
> if(doc.pmsync_usuarios)
> for(var j = 0; j < doc.pmsync_usuarios.lenght; j++)
> access(doc.pmsync_channels[i],doc.pmsync_usuarios[j]);
> if(doc.pmsync_usuario)
> access(doc.pmsync_channels[i], doc.pmsync_usuario);
> }

You can actually simplify this entire block down to
access(doc.pmsync_channels, doc.pmsync_usuarios);
since the ‘access’ function allows either parameter to be an array of strings and will iterate over them.

> My current approach for enabling access to all users for channel is to create a document for each user, using the "pmsync_todos" channel.
> {
> "pmsync_channels”:[“pmsync-todos"],
> "pmsync_usuario”:”pmsync-rafael"
> }

That will work. The app can create such a document on the first launch and then push it to the server.

Just be aware that this approach allows anyone to give anyone access to any channel, so it doesn’t allow you to use channels for access control. If you need that, you’ll have to add validation to the sync function so documents can’t set up illegal access.

—Jens

Frederic Yesid Peña Sánchez

unread,
Mar 25, 2014, 2:05:06 PM3/25/14
to mobile-c...@googlegroups.com
Thanks for your response.

We changed already the function, as we saw the order of "access" was wrong.

Additionally, we found the primary cause of the slowness of Gateway to be the same block.

Now our Sync works really fast!
Reply all
Reply to author
Forward
0 new messages