Document Changed events not being raised by SG through public API


#1

Hi there,

I’m currently learning to use SG and now want to leverage the 1.2 webhook functionality, to post every document change off to an ASP.NET WebAPI application. I have both a device layer and a web layer wherein data changes from another store are put into my SG bucket through the Public API, but I’ve noticed that when I do this, a Document_Changed event is not being raised.

Unfortunately googling information on technical problems with CB/SG comes up sparse and the official documentation doesn’t say if this behaviour is intentional or not. Document_Changed is specified as being raised whenever SG writes a document to a bucket, but this is in the Mobile documentation rather than in the Public API documentation.

Any pointers would be much appreciated!


#2

@scouser3008

Have you configured web hooks for document changes in your Sync Gateway config?

In the Sync Gateway examples directory there is events_webhook.json which shows how to configure webhooks.

If you have configured your Sync Gateway for webhooks, can you post your configuration here?

Also if you set your SG logging to include CRUD+ and Events+ and post the log entries for a document update that you expect to raise an event then we can try to debug your issue.


#3

Hi @andy,

Here’s the excerpt of my config:

"event_handlers": { "document_changed": [{ "handler": "webhook", "url": "http://somesite.thatIwanttopostto.net/api/internal", "filter":function(doc) {
return true;
}}, { "handler": "webhook", "url": "http://localhost:8000/invitecode", "filter":function(doc) {
if (doc.type == “profile” && doc.invite_code) {
return true;
}
return false;
}}] }

So it’s the first handler that I’m concerned with, the backticks wrapping my filter functions exist in the config file, this forum is editing them out. I’ll change the config now to include the extra logging parameters.

Apologies for the poor indenting, I’m not used to this forum’s editor.


#4

I added the logging parameters below and at a first glance I can see that I’m not getting any Event or Event+ log messages, but the CRUD is fine. I’ve double checked the case sensitivity against the api docs and it’s all correct.


#5

So I tested adding a document on the device, and it’s sync’d up to the bucket fine, but again no Events or Events+ log messages are being generated in the console.


#6

@scouser3008

I wrapped your event handler config in a simple SG .json config:

{
        "log": ["*"],
        "databases": {
                "db": {
                        "server": "http://localhost:8091",
                        "bucket":"bucket-1",
                        "users": {
                                "GUEST": {"disabled": true, "admin_channels": []},
                                "crashtest": {"disabled": false, "password":"crashtest", "admin_channels": ["*"]}
                        },
                        "event_handlers": {
                            "document_changed": [
                                {
                                    "handler": "webhook",
                                    "url": "http://somesite.thatIwanttopostto.net/api/internal",
                                    "filter": `function(doc) {
                                        return true;
                                    }`
                                }, {
                                    "handler": "webhook",
                                    "url": "http://localhost:8000/invitecode",
                                    "filter": `function(doc) {
                                         if (doc.type == "profile" && doc.invite_code) {
                                             return true;
                                        }
                                        return true;
                                }`
                            }
                        ]
                    }
                }
        }
}

This generated the expected log output (Note: internal URL does not exist and invitecode test filter returns false):

2016-04-21T16:52:03.986+01:00 HTTP:  #002: PUT /db/docr75  (ADMIN)
2016-04-21T16:52:03.988+01:00 CRUD+: Invoking sync on doc "docr75" rev 1-7b81dc9c46fadfb428aa983d0c3e8168
2016-04-21T16:52:04.044+01:00 CRUD: Stored doc "docr75" / "1-7b81dc9c46fadfb428aa983d0c3e8168"
2016-04-21T16:52:04.044+01:00 HTTP+: #002:     --> 201   (57.9 ms)
2016-04-21T16:52:04.044+01:00 Events+: Event queue worker sending event Document change event for doc id: docr75 to: Webhook handler [http://somesite.thatIwanttopostto.net/api/internal]
2016-04-21T16:52:04.045+01:00 Events+: Event queue worker sending event Document change event for doc id: docr75 to: Webhook handler [http://localhost:8000/invitecode]
2016-04-21T16:52:04.107+01:00 Changes+: Notifying that "bucket-1" changed (keys="{*}") count=2
2016-04-21T16:52:04.136+01:00 WARNING: Error attempting to post to url http://somesite.thatIwanttopostto.net/api/internal: Post http://somesite.thatIwanttopostto.net/api/internal: dial tcp: lookup somesite.thatIwanttopostto.net: no such host -- %!v(MISSING) -- db.(*Webhook).HandleEvent.func1() at event_handler.go:127

I’m wondering if your event handler config is nested at the right level in your config, if it is you should see the following log entries on starting SG:

2016-04-21T16:51:30.840+01:00 Events: Creating new JSEventFunction
2016-04-21T16:51:30.840+01:00 Events: Registered event handler: Webhook handler [http://somesite.thatIwanttopostto.net/api/internal], for event type 0
2016-04-21T16:51:30.840+01:00 Events: Creating new JSEventFunction
2016-04-21T16:51:30.840+01:00 Events: Registered event handler: Webhook handler [http://localhost:8000/invitecode], for event type 0
2016-04-21T16:51:30.840+01:00 Events: Starting event manager with max processes:500, wait time:5 ms

#7

Wow I’m an idiot. My event_handlers property was at wrong level of nesting, it’s all working as expected now.

Thanks very much! :slight_smile: