I’m currently trying to build a toy application using CouchDB. For a specific use case I have in mind, I’d like to follow the changes feed of a specific database.
I tried to apply the examples from the :couchbeam
repository in a livebook but I’m not receiving any messages from the changes feed. The code looks like this:
server = :couchbeam.server_connection(~c"http://localhost:5984", [{:basic_auth, {~c"admin", ~c"admin"}}])
:couchbeam.server_info(server)
{:ok, db} = :couchbeam.open_db(server, ~c"user_1")
changes_fun = fn stream_ref, f ->
IO.puts("changes_fun called")
receive do
{^stream_ref, {:done, last_seq}} ->
IO.puts("stopped, last seq is #{inspect(last_seq)}")
:ok
{^stream_ref, {:change, change}} ->
IO.puts("change row #{inspect(change)}")
f.(stream_ref, f)
{^stream_ref, error} ->
IO.puts("error ? #{inspect(error)}")
end
end
options = [:continuous, :heartbeat, stream_to: self()]
{:ok, stream_ref} = :couchbeam_changes.follow(db, options)
changes_fun.(stream_ref, changes_fun)
When I use the follow_once/2
function I’ll get the expected results back. So I assume I’m doing something wrong regarding the retrieval of events.
Just for completeness this is working:
server = :couchbeam.server_connection(~c"http://localhost:5984", [{:basic_auth, {~c"admin", ~c"admin"}}])
:couchbeam.server_info(server)
{:ok, db} = :couchbeam.open_db(server, ~c"user_1")
:couchbeam_changes.follow_once(db, [{:since, 0}])