I would like to connect to two servers in ExIrc with elixir, and I cannot find an easy solution to do so. I am fairly new to elixir, and all that I can see that I can do is use 'umbrellas' to run two apps and have them interface with each other? (I would like to use one app to connect to one IRC server, and if it has some certain words, parse the data and send to another IRC server)
Asked
Active
Viewed 367 times
5
-
1Very quickly looking at the code of `ExIrc` it seems the client is a `GenServer`, so you should just be able to start multiple clients and connect them to different servers. – Paweł Obrok Jun 25 '15 at 08:15
-
@PawełObrok hmm. As I said I am new to elixir, do you have any idea how to start multiple clients? (sorry if I am sounding so dumb in here :P) – desu Jun 25 '15 at 09:56
1 Answers
2
So to connect a single client you can do something like:
ExIrc.start!
{:ok, client} = ExIrc.Client.start_link
{:ok, handler} = ExampleHandler.start_link(nil)
ExIrc.Client.add_handler(client, handler)
ExIrc.Client.connect!(client, "chat.freenode.net", 6667)
I'm using the ExampleHandler just as the README suggests. Now if you do something like:
pass = ""
nick = "my_nick"
ExIrc.Client.logon(client, pass, nick, nick, nick)
ExIrc.Client.join(client, "#elixir-lang")
You will start seeing messages from #elixir-lang
being output to the console - that's how the ExampleHandler
is implemented, you will probably implement something else in its place.
Now nothing is stopping you from doing this a second time:
{:ok, client2} = ExIrc.Client.start_link
{:ok, handler2} = ExampleHandler.start_link(nil)
# and so on
To create a client client2
that is connected to the same or another server. To achieve what you want you'll just have to write a handler that reacts to messages from client
by calling ExIrc.Client.msg(client2, ...)
to post to the other client.

Paweł Obrok
- 22,568
- 8
- 74
- 70
-
@Obrok Hmm so I really thought this would work but its just throwing me {:error, {:already_started, PID}} - strange – desu Jun 27 '15 at 12:46
-
Oh, sorry about that. It seems the way `ExIrc.start_client!` starts and hooks the worker into its supervision tree you can only have one client. You can instead manually start the clients with `ExIrc.Client.start_link` but you probably want to make sure you hook them into your supervision tree. I'll update the answer. – Paweł Obrok Jun 27 '15 at 18:57