I think at this point the best way would be to send those values as params when connecting. That would probably mean getting the info from the Plug.Conn
and passing it to your JS somehow, or somehow doing it all client-side.
Anyway, to get it into the socket from there, you'd do either of these two things.
on the JS side at the socket level:
var socket = new Socket("/socket", {ip: "127.0.0.1", host: "localhost"})
on the JS side at the channel level:
var channel = socket.channel("topic:subtopic", {ip: "127.0.0.1", host: "localhost"})
in your socket module:
def connect(_params = %{"ip" => ip, "host" => host}, socket) do
socket =
socket
|> assign(:ip, ip)
|> assign(:host, host)
{:ok, socket}
end
or in your channel module:
def join("topic:subtopic", _params = %{ip: ip, host: host}, socket) do
socket =
socket
|> assign(:ip, ip)
|> assign(:host, host)
{:noreply, socket}
end
If you needed that information for all your channels, it would make sense to do it at the socket level. I'm pretty sure either way it ends up in the socket object, so if you're using the same socket for multiple channels, you'd see the same assigns.