Packages

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

Current section

4 Versions

Jump to

Compare versions

4 files changed
+155 additions
-116 deletions
  @@ -1,68 +1,99 @@
1 - # socketio_emitter
2 -
3 - `socketio_emitter` allows you to communicate with socket.io servers easily from Elixir processes.
4 -
5 - ## Installation
6 -
7 - The package can be installed
8 - by adding `socketio_emitter` to your list of dependencies in `mix.exs`:
9 -
10 - ```elixir
11 - def deps do
12 - [{:socketio_emitter, "~> 0.1.0"}]
13 - end
14 - ```
15 -
16 - ## How to use
17 -
18 - ```elixir
19 - defmodule ExampleApp do
20 - use Application
21 -
22 - def start(_type, _args) do
23 - import Supervisor.Spec
24 -
25 - children = [
26 - # Add this line to your supervisor tree
27 - supervisor(SocketIOEmitter, []),
28 - ]
29 -
30 - opts = [strategy: :one_for_one, name: Osame.Supervisor]
31 - Supervisor.start_link(children, opts)
32 - end
33 - end
34 - ```
35 -
36 - ## Configuration
37 -
38 - You can configure `socketio_emitter` from your `config.exs`, ex.:
39 -
40 - ```elixir
41 - use Mix.Config
42 -
43 - config :socketio_emitter, :redix_config,
44 - # default value: localhost
45 - host: "example.com",
46 - # default value: 6379
47 - port: 5000,
48 - # 5 Redix processes will be available (default value: 1)
49 - pool_size: 5
50 - ```
51 -
52 - Or passing by parameters directly to supervisor:
53 -
54 - ```elixir
55 - children = [
56 - # Add this line to your supervisor tree
57 - supervisor(SocketIOEmitter, [host: "example.com", port: 9999, password: "secret"], [name: :socket_emitter]),
58 - ]
59 - ```
60 -
61 - ## License
62 -
63 - ## TODO
64 -
65 - - tests
66 - - documentation
67 -
68 - MIT
1 + # socketio_emitter
2 +
3 + [![Build Status](https://api.travis-ci.org/chugunov/socketio_emitter.svg?branch=master)](https://travis-ci.org/chugunov/socketio_emitter)
4 + [![Hex.pm](https://img.shields.io/hexpm/v/socketio_emitter.svg)](https://hex.pm/packages/socketio_emitter)
5 +
6 + `socketio_emitter` allows you to communicate with socket.io servers easily from Elixir processes.
7 +
8 + ## Installation
9 +
10 + The package can be installed
11 + by adding `socketio_emitter` to your list of dependencies in `mix.exs`:
12 +
13 + ```elixir
14 + def deps do
15 + [{:socketio_emitter, "~> 0.1.0"}]
16 + end
17 + ```
18 +
19 + ## How to use
20 +
21 + Register `socketio_emitter` supervisor at your supervisor tree:
22 +
23 + ```elixir
24 + defmodule ExampleApp do
25 + use Application
26 +
27 + def start(_type, _args) do
28 + import Supervisor.Spec
29 +
30 + children = [
31 + # Add this line to your supervisor tree
32 + supervisor(SocketIOEmitter, []),
33 + ]
34 +
35 + opts = [strategy: :one_for_one, name: ExampleApp.Supervisor]
36 + Supervisor.start_link(children, opts)
37 + end
38 + end
39 + ```
40 +
41 + Then call `SocketIOEmitter.emit/2`:
42 +
43 + ```elixir
44 + msg = %{:text => "hello"}
45 +
46 + # sending to all clients
47 + {:ok, _consumers_count} = SocketIOEmitter.emit ["broadcast", msg]
48 +
49 + # sending to all clients in 'game' room
50 + {:ok, _consumers_count} = SocketIOEmitter.emit ["new-game", msg],
51 + rooms: ["game"]
52 +
53 + # sending to individual socketid (private message)
54 + {:ok, _consumers_count} = SocketIOEmitter.emit ["private", msg],
55 + rooms: [socket_id]
56 +
57 + # sending to all clients in 'admin' namespace
58 + {:ok, _consumers_count} = SocketIOEmitter.emit ["namespace", msg],
59 + nsp: "/admin"
60 +
61 + # sending to all clients in 'admin' namespace and in 'notifications' room
62 + {:ok, _consumers_count} = SocketIOEmitter.emit ["namespace", msg],
63 + nsp: "/admin",
64 + rooms: ["notifications"]
65 + ```
66 +
67 + ## Configuration
68 +
69 + You can configure `socketio_emitter` from your `config.exs`, ex.:
70 +
71 + ```elixir
72 + use Mix.Config
73 +
74 + config :socketio_emitter, :redix_config,
75 + # default value: localhost
76 + host: "example.com",
77 + # default value: 6379
78 + port: 5000,
79 + # 5 Redix processes will be available (default value: 1)
80 + pool_size: 5
81 + ```
82 +
83 + Or passing by parameters directly to supervisor:
84 +
85 + ```elixir
86 + children = [
87 + # Add this line to your supervisor tree
88 + supervisor(SocketIOEmitter, [host: "example.com", port: 9999, password: "secret"], [name: :socket_emitter])
89 + ]
90 + ```
91 +
92 + ## TODO
93 +
94 + - tests
95 + - documentation
96 +
97 + ## License
98 +
99 + MIT
  @@ -19,4 +19,4 @@
19 19 {<<"name">>,<<"msgpax">>},
20 20 {<<"optional">>,false},
21 21 {<<"requirement">>,<<"~> 2.0">>}]]}.
22 - {<<"version">>,<<"0.1.0">>}.
22 + {<<"version">>,<<"0.1.1">>}.
  @@ -10,7 +10,6 @@ defmodule SocketIOEmitter do
10 10 @uid "emitter"
11 11 @parser_event_type 2
12 12 @default_redix_config [pool_size: 1]
13 - @redix_conf Application.get_env(:socketio_emitter, :redix_config, @default_redix_config)
14 13
15 14 def start_link(redis_opts \\ [])
16 15 def start_link(redis_opts) do
  @@ -19,7 +18,7 @@ defmodule SocketIOEmitter do
19 18
20 19 def init(redis_opts) do
21 20 redix_workers =
22 - for i <- 0..(@redix_conf[:pool_size] - 1) do
21 + for i <- 0..(redix_config(:pool_size) - 1) do
23 22 worker(Redix, [redis_opts, [name: :"redix_#{i}"]], id: {Redix, i})
24 23 end
25 24
  @@ -70,6 +69,15 @@ defmodule SocketIOEmitter do
70 69 end
71 70
72 71 defp random_index() do
73 - "#{rem(System.unique_integer([:positive]), @redix_conf[:pool_size])}"
72 + "#{rem(System.unique_integer([:positive]), redix_config(:pool_size))}"
73 + end
74 +
75 + defp redix_config() do
76 + curr_env_config = Application.get_env(:socketio_emitter, :redix_config, [])
77 + Keyword.merge(@default_redix_config, curr_env_config)
78 + end
79 +
80 + defp redix_config(key) do
81 + redix_config()[key]
74 82 end
75 83 end
  @@ -1,44 +1,44 @@
1 - defmodule SocketioEmitter.Mixfile do
2 - use Mix.Project
3 -
4 - def project do
5 - [app: :socketio_emitter,
6 - version: "0.1.0",
7 - elixir: "~> 1.4",
8 - description: description(),
9 - package: package(),
10 - build_embedded: Mix.env == :prod,
11 - start_permanent: Mix.env == :prod,
12 - deps: deps(),
13 - source_url: "https://github.com/chugunov/socketio_emitter"]
14 - end
15 -
16 - def application do
17 - # Specify extra applications you'll use from Erlang/Elixir
18 - [extra_applications: [:logger, :redix]]
19 - end
20 -
21 - defp description do
22 - """
23 - `socketio_emitter` allows you to communicate with socket.io servers easily from Elixir processes.
24 - """
25 - end
26 -
27 - defp package do
28 - [
29 - name: :socketio_emitter,
30 - files: ["lib", "mix.exs", "README*", "LICENSE*"],
31 - maintainers: ["Andrey Chugunov"],
32 - licenses: ["MIT"],
33 - links: %{"GitHub" => "https://github.com/chugunov/socketio_emitter"}
34 - ]
35 - end
36 -
37 - defp deps do
38 - [
39 - {:ex_doc, ">= 0.0.0", only: :dev},
40 - {:redix, "~> 0.6.1"},
41 - {:msgpax, "~> 2.0"}
42 - ]
43 - end
44 - end
1 + defmodule SocketioEmitter.Mixfile do
2 + use Mix.Project
3 +
4 + def project do
5 + [app: :socketio_emitter,
6 + version: "0.1.1",
7 + elixir: "~> 1.4",
8 + description: description(),
9 + package: package(),
10 + build_embedded: Mix.env == :prod,
11 + start_permanent: Mix.env == :prod,
12 + deps: deps(),
13 + source_url: "https://github.com/chugunov/socketio_emitter"]
14 + end
15 +
16 + def application do
17 + # Specify extra applications you'll use from Erlang/Elixir
18 + [extra_applications: [:logger, :redix]]
19 + end
20 +
21 + defp description do
22 + """
23 + `socketio_emitter` allows you to communicate with socket.io servers easily from Elixir processes.
24 + """
25 + end
26 +
27 + defp package do
28 + [
29 + name: :socketio_emitter,
30 + files: ["lib", "mix.exs", "README*", "LICENSE*"],
31 + maintainers: ["Andrey Chugunov"],
32 + licenses: ["MIT"],
33 + links: %{"GitHub" => "https://github.com/chugunov/socketio_emitter"}
34 + ]
35 + end
36 +
37 + defp deps do
38 + [
39 + {:ex_doc, ">= 0.0.0", only: :dev},
40 + {:redix, "~> 0.6.1"},
41 + {:msgpax, "~> 2.0"}
42 + ]
43 + end
44 + end