5
var socket = new Socket("localhost:4000")
socket.connect()

Returns WebSocket connection to 'ws://localhost:4000/ws' failed: Error during WebSocket handshake: Unexpected response code: 404

But I do have the socket on the /ws endpoint, right?

defmodule Sapphire.Endpoint do
  use Phoenix.Endpoint, otp_app: :sapphire

  socket "/ws", Sapphire.MomentSocket

  plug Plug.Static,
    at: "/", from: :sapphire, gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt)

  if code_reloading? do
    socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
    plug Phoenix.LiveReloader
    plug Phoenix.CodeReloader
  end

  plug Plug.RequestId
  plug Plug.Logger

  plug Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Poison

  plug Plug.MethodOverride
  plug Plug.Head

  plug Plug.Session,
    store: :cookie,
    key: "_sapphire_key",
    signing_salt: "hW1bFEcR"

  plug Sapphire.Router

end

It should be able to connect to that endpoint, but for some reason it can't reach it at all.

[info] Running Sapphire.Endpoint with Cowboy on http://localhost:4000

iColor
  • 260
  • 2
  • 10
  • 3
    Because you are accessing `ws://localhost:4000/ws`, i have a hunch you are using an old Phoenix client with a recent Phoenix application. Make sure you are using Phoenix 1.0.2 and that you have the latest copy of Phoenix.js. – José Valim Sep 19 '15 at 08:12
  • @JoséValim Hmm, I've ported https://github.com/phoenixframework/phoenix/blob/master/web/static/js/phoenix.js to coffeescript in which the front-end is being served on a different server. The client autoadds the `/ws`, so I have `socket "/ws", Sapphire.MomentSocket` should that not be available on the `/ws` path? Even changing both to `/socket` returns the same 404. I'm interested in how that socket endpoint gets exposed – iColor Sep 19 '15 at 16:34
  • The URL is socket_path + transport_name. So it should be /socket/websockets or something like that. – José Valim Sep 19 '15 at 19:01

2 Answers2

6

@JoséValim found the solution.

I was porting the phoenix.js library to coffeescript and missed the fact that the suffix of the path should be whatever the transport layer is. In this case, it needed /websocket at the end in the implementation. :)

iColor
  • 260
  • 2
  • 10
0

Full disclosure -- I'm a noob, and I'm probably screwing something up, but this is how I got back on track.

rm -rf deps/phoenix

This will clear out your present version of phoenix (which includes phoenix.js)

mix do deps.get

This pulls down phoenix again.

Hopefully this is enough to get you going again at this point -- but if you screwed up a migration like me, and you're on mac/linux, do a:

find . -name phoenix.js

Voila, there is the phoenix.js fixed by Jose. I was lazy and just copied over that phoenix.js to get things going for the time being.

trevorgrayson
  • 1,806
  • 1
  • 21
  • 29