Back to Documentation

Elixir SDK Examples

Real-world examples showing how to use the OddSockets Elixir SDK with Elixir/OTP patterns

Run Basic Example
Basic Usage
Available
Simple example showing how to connect, subscribe to a channel, and publish messages using the core SDK functionality with Elixir patterns.
# Start a client
{:ok, client} = OddSockets.start_link(api_key: "ak_live_1234567890abcdef")

# Get a channel
channel = OddSockets.channel(client, "my-channel")

# Subscribe to messages
:ok = OddSockets.Channel.subscribe(channel, fn message ->
  IO.inspect(message, label: "Received")
end)

# Publish a message
{:ok, result} = OddSockets.Channel.publish(channel, %{text: "Hello World!"})
GenServer Integration
Available
Example showing how to integrate OddSockets with GenServer patterns for building fault-tolerant real-time applications.
defmodule MyApp.ChatServer do
  use GenServer

  def start_link(opts) do
    GenServer.start_link(__MODULE__, opts, name: __MODULE__)
  end

  def init(_opts) do
    {:ok, client} = OddSockets.start_link(api_key: "your-api-key")
    channel = OddSockets.channel(client, "chat-room")
    
    :ok = OddSockets.Channel.subscribe(channel, &handle_message/1)
    
    {:ok, %{client: client, channel: channel}}
  end

  defp handle_message(message) do
    IO.puts("Chat message: #{inspect(message)}")
  end
end
Phoenix LiveView
Available
Real-time chat application using Phoenix LiveView with OddSockets for seamless real-time messaging integration.
defmodule MyAppWeb.ChatLive do
  use MyAppWeb, :live_view

  def mount(_params, _session, socket) do
    {:ok, client} = OddSockets.start_link(api_key: "your-api-key")
    channel = OddSockets.channel(client, "chat-room")
    
    :ok = OddSockets.Channel.subscribe(channel, fn message ->
      send(self(), {:new_message, message})
    end)

    {:ok, assign(socket, client: client, channel: channel, messages: [])}
  end

  def handle_info({:new_message, message}, socket) do
    messages = [message | socket.assigns.messages]
    {:noreply, assign(socket, messages: messages)}
  end
end
Supervision Tree
Available
Example showing how to integrate OddSockets into your application's supervision tree for maximum fault tolerance.
defmodule MyApp.Application do
  use Application

  def start(_type, _args) do
    children = [
      # Start OddSockets client as part of supervision tree
      {OddSockets, api_key: "your-api-key", name: MyApp.OddSocketsClient},
      
      # Other supervised processes
      MyApp.Repo,
      MyAppWeb.Endpoint,
      {MyApp.ChatManager, []}
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end
Task & Agent Integration
Available
Advanced example showing async message processing with Tasks and state management with Agents for high-throughput scenarios.
defmodule MyApp.MessageProcessor do
  def start_processing(api_key) do
    {:ok, client} = OddSockets.start_link(api_key: api_key)
    channel = OddSockets.channel(client, "processing-queue")
    
    # Use Agent to store state
    {:ok, agent} = Agent.start_link(fn -> %{processed: 0, errors: 0} end)
    
    # Subscribe with async processing
    :ok = OddSockets.Channel.subscribe(channel, fn message ->
      Task.start(fn ->
        process_message_async(message, agent)
      end)
    end)
    
    {:ok, {client, channel, agent}}
  end
end
Event Handling
Available
Comprehensive example showing how to handle connection events, errors, and worker assignments with Elixir message passing.
# Subscribe to client events
:ok = OddSockets.subscribe_events(client)

# Handle events in your process
receive do
  {:oddsockets_event, :connected} ->
    IO.puts("Connected to OddSockets!")
  
  {:oddsockets_event, {:error, reason}} ->
    IO.puts("Connection error: #{inspect(reason)}")
  
  {:oddsockets_event, {:worker_assigned, info}} ->
    IO.puts("Assigned to worker: #{info.worker_id}")
  
  {:oddsockets_event, :max_reconnect_attempts_reached} ->
    IO.puts("Max reconnect attempts reached")
end