Skip to content

Instantly share code, notes, and snippets.

@martinstannard
Created August 19, 2014 04:42
Show Gist options
  • Save martinstannard/2f9b463e695327bc56e1 to your computer and use it in GitHub Desktop.
Save martinstannard/2f9b463e695327bc56e1 to your computer and use it in GitHub Desktop.
Phoenix for ember.js channel integration
# room_channel.ex
defmodule Stadium.RoomChannel do
use Phoenix.Channel
def join(socket, "lobby", _) do
reply socket, "joined", "you joined"
{:ok, socket}
end
def join(socket, _no, _message) do
{:error, socket, :unauthorized}
end
def event(socket, "ping", message) do
reply socket, "pong", "Echo: " <> message
socket
end
def event(socket, "word_list", _) do
{_, words} = JSON.encode(["dog", "cat", "pig", "cow"])
reply socket, "words", words
socket
end
end
# router
defmodule Stadium.Router do
use Phoenix.Router
use Phoenix.Router.Socket, mount: "/ws"
plug Plug.Static, at: "/static", from: :stadium
get "/", Stadium.PageController, :index, as: :page
channel "rooms", Stadium.RoomChannel
end
# add JSON support via mix.exs
defmodule Stadium.Mixfile do
use Mix.Project
def project do
[ app: :stadium,
version: "0.0.1",
elixir: "~> 0.15.0",
elixirc_paths: ["lib", "web"],
deps: deps ]
end
# Configuration for the OTP application
def application do
[
mod: { Stadium, [] },
applications: [:phoenix, :cowboy]
]
end
# Returns the list of dependencies in the format:
# { :foobar, git: "https://github.com/elixir-lang/foobar.git", tag: "0.1" }
#
# To specify particular versions, regardless of the tag, do:
# { :barbat, "~> 0.1", github: "elixir-lang/barbat" }
defp deps do
[
{:phoenix, github: "phoenixframework/phoenix"},
{:cowboy, "~> 1.0.0"},
{:json, "~> 0.3.0"}
]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment