socketio_emitter

Build StatusHex.pm

socketio_emitter allows you to communicate with socket.io servers easily from Elixir processes.

Installation

The package can be installed by adding socketio_emitter to your list of dependencies in mix.exs:

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

How to use

Register socketio_emitter supervisor at your supervisor tree:

defmodule ExampleApp do
  use Application

  def start(_type, _args) do
    import Supervisor.Spec

    children = [
      # Add this line to your supervisor tree
      supervisor(SocketIOEmitter, []),
    ]

    opts = [strategy: :one_for_one, name: ExampleApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Then call SocketIOEmitter.emit/2:

msg = %{:text => "hello"}

# sending to all clients
{:ok, _consumers_count} =  SocketIOEmitter.emit ["broadcast", msg]

# sending to all clients in 'game' room
{:ok, _consumers_count} =  SocketIOEmitter.emit ["new-game", msg],
  rooms: ["game"]
  
# sending to individual socketid (private message)
{:ok, _consumers_count} =  SocketIOEmitter.emit ["private", msg],
  rooms: [socket_id]
  
# sending to all clients in 'admin' namespace
{:ok, _consumers_count} =  SocketIOEmitter.emit ["namespace", msg],
  nsp: "/admin"
  
# sending to all clients in 'admin' namespace and in 'notifications' room
{:ok, _consumers_count} =  SocketIOEmitter.emit ["namespace", msg],
  nsp: "/admin",
  rooms: ["notifications"]

Configuration

You can configure socketio_emitter from your config.exs, ex.:

use Mix.Config

config :socketio_emitter, :redix_config,
  # default value: localhost
  host: "example.com", 
  # default value: 6379
  port: 5000,
  # 5 Redix processes will be available (default value: 1)
  pool_size: 5

Or passing by parameters directly to supervisor:

children = [
  # Add this line to your supervisor tree
  supervisor(SocketIOEmitter, [host: "example.com", port: 9999, password: "secret"], [name: :socket_emitter])
]

TODO

License

MIT