r/elixir•
WombatScared

[Question] - Atoms / Returning

Hey guys!

I am really starting to love maps and the syntax of Elixir. As you know my JavaScript problem with objects can easily be fixed with this: http://elixir-lang.org/docs/stable/elixir/Map.html (Just found about this and the documentation is amazing)

The entire documentation is there and I love it and can manipulate my lists accordingly.

In any event, I have some questions regarding the code from here:

defmodule App.MyChannel do
use Phoenix.Channel

def join(socket, "chat_room", message) do
socket = assign(socket, :name, message["name"])

defmodule App.MyChannel do
use Phoenix.Channel

def join(socket, "chat_room", message) do
socket = assign(socket, :name, message["name"])

{:ok, socket}
end

def event(socket, "new:message", message) do
user_name = get_assign(socket, :name)

broadcast socket, "message:new", %{
name: user_name,
content: message["content"]
}

socket
end

def leave(socket, message) do
socket
end
end
  • Inside the def join block, what is the purpose of {:ok, socket}?
  • Is the last line inside a def block, always the return line? I come from a JS background so I kept looking for a return keyword :P
  • And in the def leave block, I don't get what is going on there. Is the def leave function just returning the socket so Phoenix can then remove it internally (inside Phoenix's framework)?
  • Lastly, in the def leave block, what is the message parameter used for?

Again, thanks withstanding my questions and whatnot, I'm really just trying to get this language down. I feel like I coming closer, and appreciate all the help.

Posted May 11, 2016, 11:49 PM UTC

15
Loading comments…