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

Inherit host & protocol from endpoint #6

Open
simonewebdesign opened this issue Jun 9, 2016 · 15 comments
Open

Inherit host & protocol from endpoint #6

simonewebdesign opened this issue Jun 9, 2016 · 15 comments

Comments

@simonewebdesign
Copy link

Hi, we just integrated elm-phoenix-socket yesterday without any issues :)

Is it possible to inherit host and protocol from socketServer? E.g.:

initPhxSocket : Phoenix.Socket.Socket Msg
initPhxSocket =
    let
        socketServer = "ws://localhost:30000/broker/private/websocket?token=123"
    in
        Phoenix.Socket.init socketServer

Would it be possible to omit the first part, so that this would work too:

socketServer = "/broker/private/websocket?token=123"

Phoenix.js does that, and the advantage for us is that we don't need to do any crazy things to change the endpoint depending on the environment.

@edevil
Copy link

edevil commented Jun 23, 2016

This would be very useful for us as well. We build containers with our application so we strive to use the same code independently of the environment.

@fbonetti
Copy link
Owner

This looks pretty straight forward. Can you open a PR?

@simonewebdesign
Copy link
Author

Honestly I'm not too sure how to go with this.

As far as I know there's no way to get document.location natively in Elm, and it's not possible to have ports in a package (as the guide says here). So the only way is to use native modules I guess?

I tried the latter approach but I'm still far from a PR, I don't know how I'm supposed to debug/test if it works. Debug.log is not really helpful as it returns <internal structure>.

Any thoughts?

@fbonetti
Copy link
Owner

Hmm... I originally thought that you could connect a WebSocket with a relative path, but according to this StackOverflow answer, you can only use absolute URLs.

Here's what I think we should do:

  1. Parse socketServer and determine if it's a relative path or an absolute URL
  2. If it's an absolute URL, use it as is
  3. If it's a relative path, combine it with the current location host to form an absolute URL

You can get the current location by using this package:
http://package.elm-lang.org/packages/elm-lang/navigation/1.0.0/Navigation

@fbonetti
Copy link
Owner

Generally speaking, I feel like this is something that should be handled by environment variables instead of the library itself. Determining if the socket server is relative vs absolute will work for some environments, but not for all. For example, your development environment may run on localhost:3000, but in production you have a server that serves the application (myapp.com) and another that handles the API/websockets (api.myapp.com).

Doing this with node and webpack is easy. You can use something like dotenv to load your configuration into environment variables. I don't know if something like this exists for Elm yet.

@edevil
Copy link

edevil commented Jun 29, 2016

I wonder why phoenix.js can get away with relative URLs for the websocket (https://github.com/phoenixframework/phoenix/blob/master/web/static/js/phoenix.js#L492). Is it a limitation of Elm's Websocket class?

@fbonetti
Copy link
Owner

They use a regex to determine if the provided URL is a relative or absolute path. If it's a relative path, they append the location.host, which is what we would need to do as well.

https://github.com/phoenixframework/phoenix/blob/master/web/static/js/phoenix.js#L490-L499

@cloud8421
Copy link

@simonewebdesign Can't you get around this by passing the location as a program flag and keeping the websocket url in the model? See http://package.elm-lang.org/packages/elm-lang/html/1.1.0/Html-App#programWithFlags (hello btw! 👋 )

@fbonetti
Copy link
Owner

fbonetti commented Jul 8, 2016

@cloud8421 That's actually not a bad idea. Ideally I'd like this library to mirror the functionality of the phoenix.js library, but integrating the navigation package seems like kind of a pain. Rather than providing for the current path as a subscription, it requires you to use an entirely separate program function.

@fbonetti
Copy link
Owner

fbonetti commented Jul 8, 2016

If anyone has an ideas on how to cleanly integrate navigation into this library, please let me know or open a PR.

@note89
Copy link

note89 commented Jul 11, 2016

+1 for this

@lessless
Copy link

lessless commented Aug 9, 2016

Is there any progress on the issue folks?

@simonewebdesign
Copy link
Author

simonewebdesign commented Aug 9, 2016

In our project we use navigation, so we ended up using Location from there.

@note89
Copy link

note89 commented Feb 15, 2017

I don't want to be able to have multiple URLs go to my site.
Doing a migration right now, so WebSockets for ahead.life and evryapp.com needs to work for both in the transition period.
So i think i need need to determine this at runtime.
@simonewebdesign can you provide a bit of info how you did this ?

@simonewebdesign
Copy link
Author

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

No branches or pull requests

7 participants