elm-phoenix-socket icon indicating copy to clipboard operation
elm-phoenix-socket copied to clipboard

How to share socket between child components?

Open opsb opened this issue 9 years ago • 1 comments
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?

opsb avatar Jul 26 '16 16:07 opsb

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

maxstr avatar Mar 20 '17 07:03 maxstr