Last active
August 29, 2015 14:11
-
-
Save jurre/28796f8c4ad9e4122f18 to your computer and use it in GitHub Desktop.
Phoenix framework events error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function () { | |
var $messages = $("#messages"); | |
var $messageInput = $("#message-input"); | |
var socket = new Phoenix.Socket("/ws"); | |
socket.join("rooms", "public", {}, function (channel) { | |
channel.on("new:message", function (message) { | |
console.log(message); | |
$messages.append("<li>" + message.content + "</li>"); | |
}); | |
channel.on("error", function(error) { | |
console.log("Error :( Reason: " + error.reason); | |
}); | |
$messageInput.off("keypress").on("keypress", function (e) { | |
if (e.keyCode === 13) { | |
console.log("SEND: " + $messageInput.val()); | |
channel.send("new:message", { | |
content: $messageInput.val() | |
}); | |
$messageInput.val(""); | |
} | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Chatdown.Router do | |
use Phoenix.Router | |
use Phoenix.Router.Socket, mount: "/ws" | |
channel "rooms", Chatdown.Rooms | |
# .. | |
end | |
# web/channels/rooms_channel.ex | |
defmodule Chatdown.Rooms do | |
use Phoenix.Channel | |
def join(socket, "public", message) do | |
IO.puts "JOIN #{socket.channel}:#{socket.topic}" | |
{:ok, socket} | |
end | |
def event(socket, "new:message", %{ message: message }) do | |
IO.puts "new:message" | |
broadcast socket, "new:message", %{ content: message["content"] } | |
socket | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10:19:10.232 [error] #PID<0.248.0> running Chatdown.Endpoint terminated | |
Server: localhost:4000 (http) | |
Request: GET /ws | |
** (exit) an exception was raised: | |
** (ErlangError) erlang error: [reason: :function_clause, mfa: {Phoenix.Endpoint.CowboyHandler, :websocket_handle, 3}, stacktrace: [{Chatdown.Rooms, :event, [%Phoenix.Socket{assigns: %{}, authorized: true, channel: "rooms", conn: nil, pid: #PID<0.248.0>, router: Chatdown.Router, topic: "public"}, "new:message", %{"content" => "Hey!"}], [file: 'web/channels/rooms_channel.ex', line: 9]}, {Phoenix.Channel.Transport, :dispatch, 4, [file: 'lib/phoenix/channel/transport.ex', line: 102]}, {Phoenix.Channel.Transport, :dispatch, 4, [file: 'lib/phoenix/channel/transport.ex', line: 67]}, {Phoenix.Transports.WebSocket, :ws_handle, 2, [file: 'lib/phoenix/transports/websocket.ex', line: 45]}, {Phoenix.Endpoint.CowboyHandler, :websocket_handle, 3, [file: 'lib/phoenix/endpoint/cowboy_handler.ex', line: 65]}, {:cowboy_websocket, :handler_call, 7, [file: 'src/cowboy_websocket.erl', line: 588]}, {Phoenix.Endpoint.CowboyHandler, :upgrade, 4, [file: 'lib/phoenix/endpoint/cowboy_handler.ex', line: 17]}, {:cowboy_protocol, :execute, 4, [file: 'src/cowboy_protocol.erl', line: 435]}], msg: {:text, "{\"channel\":\"rooms\",\"topic\":\"public\",\"event\":\"new:message\",\"message\":{\"content\":\"Hey!\"}}"}, req: [socket: #Port<0.5912>, transport: :ranch_tcp, connection: :keepalive, pid: #PID<0.248.0>, method: "GET", version: :"HTTP/1.1", peer: {{127, 0, 0, 1}, 61714}, host: "localhost", host_info: :undefined, port: 4000, path: "/ws", path_info: :undefined, qs: "", qs_vals: :undefined, bindings: [], headers: [{"upgrade", "websocket"}, {"connection", "Upgrade"}, {"host", "localhost:4000"}, {"origin", "http://localhost:4000"}, {"cookie", "__utma=111872281.1407948937.1388959466.1388959466.1388959466.1; _ga=GA1.1.1407948937.1388959466; _chatdown_key=bHpabmpUS2xmT04vZXJUQXU0TVB1clN4Y21aeGQ0YjlONFJBNkF2RVBhWT0tLXl2YStxZW4zNXFTVUFMNUo1NDVHdmc9PQ==--KwQmg7LtqKlJrFdnOEWM2l8OEEo=; cart=3; remember_token=M8x9Q33aZwDC7L_mQLf5eQ"}, {"pragma", "no-cache"}, {"cache-control", "no-cache"}, {"sec-websocket-key", "T7151EulXBMhcgUv7YKh2Q=="}, {"sec-websocket-version", "13"}, {"sec-websocket-extensions", "x-webkit-deflate-frame"}, {"user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like Gecko) Version/8.0.2 Safari/600.2.5"}], p_headers: [{"sec-websocket-extensions", [{"x-webkit-deflate-frame", []}]}, {"upgrade", ["websocket"]}, {"connection", ["upgrade"]}], cookies: :undefined, meta: [websocket_version: 13, websocket_compress: false], body_state: :waiting, buffer: "", multipart: :undefined, resp_compress: false, resp_state: :done, resp_headers: [], resp_body: "", onresponse: :undefined], state: {Phoenix.Transports.WebSocket, %{router: Chatdown.Router, serializer: Phoenix.Transports.JSONSerializer, sockets: #HashDict<[{{"rooms", "public"}, %Phoenix.Socket{assigns: %{}, authorized: true, channel: "rooms", conn: nil, pid: #PID<0.248.0>, router: Chatdown.Router, topic: "public"}}]>}}] | |
(phoenix) lib/phoenix/endpoint/cowboy_handler.ex:17: Phoenix.Endpoint.CowboyHandler.upgrade/4 | |
(cowboy) src/cowboy_protocol.erl:435: :cowboy_protocol.execute/4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So thanks to Jose Valim on irc I figured out that I needed:
Or, alternatively: