elm-phoenix-socket
elm-phoenix-socket copied to clipboard
How to share socket between child components?
trafficstars
The library looks great, but perhaps because I'm fairly new to Elm in general I'm struggling to find a pattern that will allow me to share a socket between several child components (each would have their own channel). Do you have any guidance?
This is something I've struggled with as well. I'd like to come up with a clear story here and I think #25 is necessary to do it correctly
I think what'd you want is some function like initializeSocket in all of your submodules that would look something like this.
Submodule.elm
initializeChannels : Socket msg -> (Msg -> msg) -> (Socket msg, Cmd msg)
initializeChannels socket fn =
let
channelName = "room:lobby"
channel = Channel.map fn <| Channel.init channelName
lobbyMessageHandler : Socket msg -> Socket msg
lobbyMessageHandler = Socket.on "new_msg" channelName (fn << LobbyMsg)
in
Socket.join channel
|> lobbyMessageHandler