@erikbackman Setting up a websocket connection happens in Effect
, which means that you can do it anywhere you can run effectful code. That’s certainly the case in your main function, but it’s also the case in your Halogen components (so long as the component can be run in Aff
or Effect
).
We can run with @Benjmhart’s suggestion and say that your component receives the connection URL as input. This would be a fairly common type to represent that:
type Input = { url :: String }
component :: H.Component HH.HTML query Input output Aff
-- or
component :: MonadAff m => H.Component HH.HTML query Input output m
This component can, when it initializes, start the websocket connection. For example, you might do something like this:
data Action = Initialize
component = ...
{ initializer = Just Initialize
, handleAction = handleAction
}
where
handleAction = case _ of
Initialize -> do
{ url } <- H.get
H.liftEffect do
-- whatever code you want to run in Effect, like open
-- a websocket at the provided `url`, just like you
-- would write in your `main` function
I haven’t done any work with websockets myself so I’m not sure exactly what you need to do here. Even so, this is how you might pass the URL the user needs to connect to down to a component which actually starts up that connection. You may need to add a finalizer which closes the websocket when the component unmounts.
As far as further examples go, these may be useful to you (they use purescript-web-socket
):
I often look for example projects using libraries that I’m also using by searching the library name in GitHub and filtering by language:purescript
. For example, to find projects using websocket-simple
, you can search purescript-websocket-simple language:purescript
.
As a side note, the purescript-web
organization supplies types and low-level implementations for various specs, but the libraries aren’t much fun to use. If websocket-simple
can do what you need there’s no reason to prefer the purescript-web
libraries.