Skip to content

Elixir client

Electric has an Elixir client and deep Phoenix integration.

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:

elixir
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:

elixir
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 has deep integration into the Phoenix framework via the official Phoenix.Sync library.

See the Phoenix integration page for more details.