satom99 / phx_raws

Raw websocket server on top of Phoenix.
https://elixirforum.com/t/9362
30 stars 6 forks source link

howto to use sockets #2

Open Licenser opened 6 years ago

Licenser commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

HI! which url is right? @Licenser

Licenser commented 6 years ago

huh?

dcy commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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

satom99 commented 6 years ago

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 commented 6 years ago

@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 commented 2 years ago

@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?