Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to share socket between child components? #18

Open
opsb opened this issue Jul 26, 2016 · 1 comment
Open

How to share socket between child components? #18

opsb opened this issue Jul 26, 2016 · 1 comment
Labels

Comments

@opsb
Copy link

opsb commented Jul 26, 2016

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?

@maxstr
Copy link
Collaborator

maxstr commented Mar 20, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants