From 9754d22245ed862350a7cdd167ddb64e155c8c39 Mon Sep 17 00:00:00 2001 From: GiuseppeSaluto Date: Wed, 21 Oct 2026 21:17:50 +0200 Subject: [PATCH] Add Elixir example Minimal client using WebSockex and the JSON module from the Elixir 1.18 standard library. Dependencies are installed by Mix.install, so it runs with `elixir main.exs`. The API key is read from AISSTREAM_API_KEY, the subscription is re-sent on every reconnection, and reconnection backs off up to 30 seconds. --- README.md | 6 +++++ elixir/README.md | 9 +++++++ elixir/main.exs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 elixir/README.md create mode 100644 elixir/main.exs diff --git a/README.md b/README.md index 96a35cb..4ac472e 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,12 @@ To build the C++ examples, you will need to have [CMake 3.20+](https://cmake.org To build the Rust example, you will need to have [Rust 1.77.2+](https://www.rust-lang.org). To run the Rust example, define the environment variable `AISSTREAM_API_KEY` with your AISStream API key. +### Elixir Example + +To run the Elixir example, you will need to have [Elixir 1.18+](https://elixir-lang.org/install.html) installed on your system. +To run the Elixir example, define the environment variable `AISSTREAM_API_KEY` with your AISStream API key. +Run the example: `cd example/elixir` and `elixir main.exs`. + ### Additional Resources For more information on the AISStream API, visit the [AISStream documentation](https://aisstream.io/documentation). diff --git a/elixir/README.md b/elixir/README.md new file mode 100644 index 0000000..5263291 --- /dev/null +++ b/elixir/README.md @@ -0,0 +1,9 @@ +# Elixir + +Requires Elixir 1.18+ (for the built-in `JSON` module). + +```sh +AISSTREAM_API_KEY= elixir main.exs +``` + +Dependencies are installed on the first run by `Mix.install`. diff --git a/elixir/main.exs b/elixir/main.exs new file mode 100644 index 0000000..73ff897 --- /dev/null +++ b/elixir/main.exs @@ -0,0 +1,63 @@ +Mix.install([{:websockex, "~> 0.5.1"}]) + +defmodule AisStream do + use WebSockex + + def start_link(api_key) do + WebSockex.start_link("wss://stream.aisstream.io/v0/stream", __MODULE__, api_key, + handle_initial_conn_failure: true + ) + end + + # aisstream closes the connection if the subscription doesn't arrive within a few seconds, + # on every (re)connection. handle_connect/2 can't send frames, so we message ourselves. + @impl true + def handle_connect(_conn, api_key) do + send(self(), :subscribe) + {:ok, api_key} + end + + @impl true + def handle_info(:subscribe, api_key) do + subscription = %{ + APIKey: api_key, + # WebSockex doesn't support permessage-deflate, which aisstream needs to serve + # full bandwidth, so keep the area small. For the whole world use a client with compression. + BoundingBoxes: [[[43.0, 7.5], [44.6, 10.5]]], + FilterMessageTypes: ["PositionReport"] + } + + {:reply, {:text, JSON.encode!(subscription)}, api_key} + end + + # aisstream sends JSON in binary frames + @impl true + def handle_frame({_type, message}, api_key) do + case JSON.decode!(message) do + %{"error" => error} -> + IO.puts("Error: #{error}") + System.halt(1) + + %{"MessageType" => "PositionReport", "Message" => %{"PositionReport" => report}} -> + IO.puts( + "ShipId: #{report["UserID"]} Latitude: #{report["Latitude"]} Longitude: #{report["Longitude"]}" + ) + + _ -> + :ok + end + + {:ok, api_key} + end + + @impl true + def handle_disconnect(%{attempt_number: attempt, reason: reason}, api_key) do + # aisstream also closes without an error message when the API key is invalid + IO.puts("Disconnected (#{inspect(reason)}), reconnecting...") + Process.sleep(min(attempt * 1_000, 30_000)) + {:reconnect, api_key} + end +end + +{:ok, _pid} = AisStream.start_link(System.fetch_env!("AISSTREAM_API_KEY")) +Process.sleep(:infinity)