Elixir client
Electric provides an Elixir client that wraps the HTTP API into a higher-level stream interface and a Phoenix integration that adds sync to your Phoenix application.
How to use
The Electric.Client
library allows you to stream Shapes into your Elixir application. It's published to Hex as the electric_client
package.
Stream
The client exposes a stream/3
that streams a Shape Log into an Enumerable
:
Mix.install([:electric_client])
{:ok, client} = Electric.Client.new(base_url: "http://localhost:3000")
stream = Electric.Client.stream(client, "my_table", where: "something = true")
stream
|> Stream.each(&IO.inspect/1)
|> Stream.run()
You can materialise the shape stream into a variety of data structures. For example by matching on insert, update and delete operations and applying them to a Map or an Ecto struct. (See the Redis example example and Typescript Shape class for reference).
Ecto queries
The stream/3
function also supports deriving the shape definition from an Ecto.Query
:
import Ecto.Query, only: [from: 2]
query = from(t in MyTable, where: t.something == true)
stream = Electric.Client.stream(client, query)
See the documentation at hexdocs.pm/electric_client for more details.
Phoenix integration
Electric also provides an Electric.Phoenix
integration allows you to:
- sync data into a front-end app from a Postgres-backed Phoenix application; and
- add real-time streaming from Postgres into Phoenix LiveView via Phoenix.Streams
See the Phoenix framework integration page for more details.