There are cases where duplicate Events on Notehub are going to occur by the design of the Notecard/Notehub interaction model. This interaction model is designed to be robust to data loss by default.
If a Notefile sync is interrupted by a note.add
it can still result in duplicate events on Notehub.
Common Causes
We most often see this when either:
-
The device tries to “stream” data up to the cloud, that is push data at short intervals.
-
The device batches note.add
statements, and it includes the "sync":true
option.
Resolutions
“Streaming” Data to Notehub
Chances are, if the application is designed to upload data at a relatively high frequency, then the Notecard is connected to Notehub with {"req":"hub.set","mode":"continuous"}
or some variation that enables a continuous connection connection to Notehub.
If that’s the case, we recommend either using
{"req":"note.add","live":true,"file":"mydata.qo","body":{...}}
or using proxied web requests
{"req":"web.post","body":{...}}
The second option does require a Notehub web request proxy route to be configured.
For more information, check out Web Transactions - Blues Developers
Batching Data to Notehub
In this case, the host MCU stores a set of data, and then only provides it to Notecard when it’s ready to upload the data to the cloud. This could be triggered by time, some local event or condition, or a command initiated from Notehub.
Often the host MCU firmware function that adds the data to Notecard has a loop that performs a note.add
request on each loop to append new data sets to a Notefile. Something like
while more_data {
...
{"req":"note.add","file":"mydata.qo","sync":true,"body":{...}}
more_data = dataSent < totalDataAvailable
}
where a note.add
occurs on each pass of the loop.
If the request contains "sync":true
this could cause any following syncs to stomp on the previous sync depending on the timing of the loop. A variation of this is a {"req":"hub.sync"}
is made for each pass through the loop.
Our recommendation here would be to remove the "sync":true
from the note.add
request and put a {"req":"hub.sync"}
request upon loop completion.
(If you want to be tricky, you could set "sync":true
on the last pass through the loop, but that’s a little more complicated)
Continuous Mode Options
In addition to the hub.sync
option above, if the device is batching data to Notehub, and the Notecard connection to Notehub is in a continuous connection, you have the option to use
{"req":"note.add","live":true,...}
Similar to the “streaming” option above, it will push the data to Notehub.
OR
You could also use card.binary
in the loop to append the data to the binary storage on the Notecard, and then do a single note.add
.
{"req":"note.add","file":"mydata.qo","binary":true,"live":true}
See Sending and Receiving Large Binary Objects - Blues Developers for more info.