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

howto to use sockets #2

Open
Licenser opened this issue Nov 13, 2017 · 10 comments
Open

howto to use sockets #2

Licenser opened this issue Nov 13, 2017 · 10 comments

Comments

@Licenser
Copy link

Hi first of all thanks for creating this, I was quite shocked to see that WebSockets are not supported in Phoenix and relieved to find this :D

That said for those of us less versed in Phoenix it would be really awesome to add a line or two how to add this to the routes.

@Licenser
Copy link
Author

I should follow up here the info I was missing here was:

defmodule Example.Endpoint do
  use Phoenix.Endpoint, otp_app: :example
  socket "/websockets", Example.Gateway

  # ...
end

I can make a PR to include that in the readme but it might be simpler just to copy&paste that snippet :)

@satom99
Copy link
Owner

satom99 commented Nov 13, 2017

I agree a better example would help a lot, this is something I will tackle as I'm done with #1. 🙂

For the record and in the meantime, I would like to reference a topic you created on the forum that might be helpful for other devs to read - specifically the marked answer as it contains a nice example.

@dcy
Copy link

dcy commented Dec 15, 2017

HI! which url is right? @Licenser

@Licenser
Copy link
Author

huh?

@dcy
Copy link

dcy commented Dec 15, 2017

new Websocket("url")
var ws = new WebSocket("ws://localhost:4001/websockets") and var ws = new WebSocket("ws://localhost:4001/websockets/websocket") don't work.

@satom99
Copy link
Owner

satom99 commented Dec 15, 2017

@dcy this is what Phoenix sets as your socket path. It essentially joins the string given on the socket macro and the atom provided on the transports macro.

That-is, if you have socket "", Example.Gateway and transports :gateway, ... then your path would be an empty string joined with the stringified atom, thus /gateway.

@dcy
Copy link

dcy commented Dec 15, 2017

What's the diffrence between phx_raw and Custom dispatch options?

@satom99
Copy link
Owner

satom99 commented Dec 15, 2017

Essentially phx_raws serves as an utility for those who don't want to both have to add an own cowboy handler nor configure the dispatch options every time a socket is added or a new project to be started.

Besides, this follows Phoenix' semantics and uses its code - thus no functionality is re-implemented unnecessarily.

@Licenser
Copy link
Author

@dcy sorry I don't remember, I ended up using the custom dispatch as even with phx_raws phoenix kept messing with my code so I couldn't use it. (not phx_raw's fault!)

@bernardo-martinez
Copy link

@dcy sorry I don't remember, I ended up using the custom dispatch as even with phx_raws phoenix kept messing with my code so I couldn't use it. (not phx_raw's fault!)

how did you do the dispatch to be compatible with existing Router/Endpoint in your project? can you share an example?

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

4 participants