Pollin

Simple queue implementation for webhooks and event sources. (Pollin package designed for only callbacks and event sourcing for elixir/erlang applications.)

Motivation

Webhook definition from wikipedia: “Webhooks are “user-defined HTTP callbacks”. They are usually triggered by some event, such as pushing code to a repository or a comment being posted to a blog. When that event occurs, the source site makes an HTTP request to the URI configured for the webhook.”

And a little event sourcing (https://ookami86.github.io/event-sourcing-in-practice/)

  • Events might have state and sub events

  • Webhooks should respond 200 whenever the request gathered from client. You can even skip authentication/authorization and handle the all the request from the queue.

  • Webhook request should processed using a queue engine.

  • Queue engine should support; pop, fetch, update, delete, dump and reset operations.

  • Allowing dynamic queue creation and other CRUD actions on queue.

  • Extendable backends

Installation

If the package can be installed as:

  1. Add pollin to your list of dependencies in mix.exs:

    def deps do
      [{:pollin, "~> 0.1.0"}]
    end
  2. Ensure pollin is started before your application:

    def application do
      [applications: [:pollin]]
    end

Usage

Endpoint Resources

# Add alias to your module (optional)
alias Pollin.Backend.Memory.EndpointWorker
alias Pollin.Resource.Endpoint
  • Create an endpoint
id = "some_id_for_endpoint"
endpoint = %Endpoint{id: id, secret: "some secret", ttl: 900_000, created_at: :os.system_time}
EndpointWorker.create(endpoint)
  • Update an endpoint
id
|> EndpointWorker.find
|> Map.put(:secret, "new secret")
|> EndpointWorker.update
  • Delete an endpoint
EndpointWorker.delete(id)
  • List all endpoints
EndpointWorker.index
  • Find an endpoint
endpoint = EndpointWorker.find(id)

Callbacks Resources(WebHooks / Event Sources)

  • Push a callback resource to an endpoint
CallbackWorker.push(id, %Pollin.Resource.Callback{})
  • Pop a callback from endpoint
callback = CallbackWorker.pop(id)
  • Pop a callback by an exact key from endpoint
callback = CallbackWorker.pop(id, key)
  • Pop list of callbacks from an endpoint queue by given status, offset and limit
callbacks = CallbackWorker.pop(id, opts)
  • Pop list of callbacks from endpoint queue by given options offset and limit
callbacks = CallbackWorker.pop(id, opts)
  • Pop a callback from endpoint
callback = CallbackWorker.fetch(id)
  • Pop a callback by an exact key from endpoint
callback = CallbackWorker.fetch(id, key)
  • Fetch list of callbacks from an endpoint queue by given status, offset and limit
callbacks = CallbackWorker.fetch(id, opts)
  • Fetch list of callbacks from endpoint queue by given offset and limit
callbacks = CallbackWorker.fetch(id, opts)
  • Fetch reverse list of callbacks from an endpoint queue by given status, offset and limit
callbacks = CallbackWorker.fetch(id, opts)
  • Fetch reverse list of callbacks from endpoint queue by given offset and limit
callbacks = CallbackWorker.fetch(id, opts)
  • Count all callbacks of an endpoint
count = CallbackWorker.count(id)
  • Count all callbacks of an endpoint with filter options
count = CallbackWorker.count(id, %{status: "unprocessed"})
  • Dump all callbacks of an endpoint
callbacks = CallbackWorker.count(id)
  • Reset all callbacks of an endpoint
CallbackWorker.reset(id)
  • Remove a callback
CallbackWorker.delete(id, key)
  • Update status of a callback
CallbackWorker.update_status(id, key, status)

Backends

You can implement your own backend using Pollin.CallbackInterface behaviour.

Contribution

Issues, Bugs, Documentation, Enhancements

1) Fork the project

2) Make your improvements and write your tests.

3) Make a pull request.

Todo

  • [ ] Implement Phoenix Framework API endpoints

  • [ ] Implement base Plug API endpoints

  • [ ] Filters

  • [ ] Add more backends like redis, cassandra, memcache, other dbs…

License

MIT