wSocket SDK for Elixir

Official Elixir SDK for wSocket — realtime pub/sub, presence, history, and push notifications.

Hex.pmLicense: MIT

Installation

Add wsocket_io to your mix.exs dependencies:

def deps do
  [
    {:wsocket_io, "~> 0.1.0"}
  ]
end

Then run:

mix deps.get

Quick Start

{:ok, client} = WSocketIO.Client.start_link("wss://node00.wsocket.online", "your-api-key")

WSocketIO.Client.on_connect(client, fn ->
  IO.puts("Connected!")
end)

channel = WSocketIO.Client.channel(client, "chat")

WSocketIO.Channel.subscribe(channel, fn data, meta ->
  IO.inspect(data, label: "Received")
end)

WSocketIO.Channel.publish(channel, %{text: "Hello from Elixir!"})

Presence

channel = WSocketIO.Client.channel(client, "room")

WSocketIO.Presence.enter(channel, data: %{name: "Alice"})

WSocketIO.Presence.on_enter(channel, fn member ->
  IO.puts("#{member.client_id} entered")
end)

WSocketIO.Presence.on_leave(channel, fn member ->
  IO.puts("#{member.client_id} left")
end)

WSocketIO.Presence.get(channel)
WSocketIO.Presence.on_members(channel, fn members ->
  IO.puts("Online: #{length(members)}")
end)

History

WSocketIO.Channel.history(channel, limit: 50)
WSocketIO.Channel.on_history(channel, fn result ->
  Enum.each(result.messages, fn msg ->
    IO.puts("#{msg.publisher_id}: #{inspect(msg.data)}")
  end)
end)

Push Notifications

push = WSocketIO.Push.new("https://node00.wsocket.online", "secret", "app1")

# Register FCM device
WSocketIO.Push.register_fcm(push, device_token: fcm_token, member_id: "user-123")

# Send to a member
WSocketIO.Push.send_to_member(push, "user-123",
  payload: %{title: "New message", body: "You have a new message"})

# Broadcast
WSocketIO.Push.broadcast(push, payload: %{title: "Announcement", body: "Server update"})

# Channel targeting
WSocketIO.Push.add_channel(push, "subscription-id", "alerts")
WSocketIO.Push.remove_channel(push, "subscription-id", "alerts")

# VAPID key
{:ok, key} = WSocketIO.Push.get_vapid_key(push)

# List subscriptions
{:ok, subs} = WSocketIO.Push.list_subscriptions(push, "user-123")

Requirements

License

MIT