Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
9 changes: 9 additions & 0 deletions elixir/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Elixir

Requires Elixir 1.18+ (for the built-in `JSON` module).

```sh
AISSTREAM_API_KEY=<your key> elixir main.exs
```

Dependencies are installed on the first run by `Mix.install`.
63 changes: 63 additions & 0 deletions elixir/main.exs
Original file line number Diff line number Diff line change
@@ -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)