Public API for the ElixirTorrent BitTorrent engine.
Start the OTP application before calling any function:
Application.ensure_all_started(:elixir_torrent)Then add torrents with download/1, poll progress with stats/2, and shut down
cleanly with stop_and_serialize/1 when you need session state preserved on disk.
Session persistence
Session snapshots are written to .elixir_torrent/state/{hex_info_hash}.term
under File.cwd!/0. Calling download/1 for a torrent with an existing session
loads the saved bitfield and verifies pieces on disk before resuming.
Use stop_and_serialize/1 (or stop_all_and_serialize/0) before your app exits.
Use remove/2 when you want to drop a torrent from the active session; pass
delete_data: true to also delete downloaded files.
Public functions
download/1— start a download from a local.torrentpathstats/2— runtime stats as a map (preferred overget/2)list/0— info hashes for all active torrent processeslist_files/1— per-file download progressstop_and_serialize/1— graceful stop and persist sessionstop_all_and_serialize/0— graceful stop and persist for every torrentremove/2— stop and remove from session; optionaldelete_data: trueget/2— low-level field accessversion/0— peer ID prefix advertised to peers (BEP 20)main/1— escript CLI entrypoint
See the README for a full quick-start guide.
Summary
Functions
Starts downloading a .torrent file from the given local path.
Low-level getter that reads fields from the internal torrent model.
Returns info hashes for all active torrent processes.
Lists files in a torrent with per-file download progress.
Starts the CLI loop used by the escript entrypoint.
Stops a torrent and removes it from the active session.
Returns selected runtime stats for a running torrent process.
Gracefully stops every active torrent and persists session state for each one.
Gracefully stops a torrent and persists its session state to disk.
Returns the peer ID prefix advertised to other peers (BEP 20).
Types
@type file_entry() :: %{ index: non_neg_integer(), path: String.t(), name: String.t(), length: non_neg_integer(), downloaded: non_neg_integer(), progress: float(), complete?: boolean() }
Per-file progress entry returned by list_files/1.
Keys: :index, :path, :name, :length, :downloaded, :progress, :complete?.
@type info_hash() :: binary()
20-byte torrent info hash.
Functions
Starts downloading a .torrent file from the given local path.
If a session file already exists for this torrent's info hash, progress is restored after verifying pieces on disk.
Returns {:ok, pid} on success or {:error, reason} on failure.
Example
{:ok, pid} = ElixirTorrent.download("/tmp/file.torrent")
Low-level getter that reads fields from the internal torrent model.
Prefer stats/2 when you need runtime statistics as a map.
@spec list() :: [info_hash()]
Returns info hashes for all active torrent processes.
@spec list_files(info_hash()) :: [file_entry()]
Lists files in a torrent with per-file download progress.
See file_entry/0 for the shape of each returned entry.
Starts the CLI loop used by the escript entrypoint.
Stops a torrent and removes it from the active session.
Deletes the on-disk session file. Pass delete_data: true to also remove
downloaded files from disk.
Example
ElixirTorrent.remove(hash)
ElixirTorrent.remove(hash, delete_data: true)
Returns selected runtime stats for a running torrent process.
Default fields: :name, :speed, :downloaded, :bytes_size.
Example
ElixirTorrent.stats(pid, [:name, :speed, :downloaded, :bytes_size])
#=> {:ok, %{name: "ubuntu.iso", speed: %{download: 1200, upload: 80}, ...}}Returns {:error, :torrent_not_found} if the pid is not a live torrent process.
@spec stop_all_and_serialize() :: :ok
Gracefully stops every active torrent and persists session state for each one.
Same shutdown sequence as stop_and_serialize/1, applied to all running torrents.
Gracefully stops a torrent and persists its session state to disk.
Steps: stop downloads → disconnect peers → tracker event=stopped →
write .elixir_torrent/state/{hash}.term → stop the torrent process.
Returns :ok if the torrent is not running (already stopped).
Returns the peer ID prefix advertised to other peers (BEP 20).
The full 20-byte peer ID is this prefix, a hyphen, and random bytes.