{"action":"created","comment":{"url":"https://api.github.com/repos/phoenixframework/phoenix/pulls/comments/25665731","id":25665731,"diff_hunk":"@@ -0,0 +1,160 @@\n+defmodule Phoenix.Socket.Supervisor do\n+  @moduledoc false\n+  use Supervisor\n+  alias Phoenix.Socket\n+\n+  def start_link do\n+    Supervisor.start_link(__MODULE__, :ok, name: __MODULE__)\n+  end\n+\n+  def start_child(%Socket{} = socket) do\n+    Supervisor.start_child(__MODULE__, [socket])\n+  end\n+\n+  def terminate_child(child) do\n+    Supervisor.terminate_child(__MODULE__, child)\n+  end\n+\n+  def init(:ok) do\n+    children = [\n+      worker(Phoenix.Socket.Server, [], restart: :temporary)\n+    ]\n+    supervise(children, strategy: :simple_one_for_one)\n+  end\n+end\n+\n+\n+defmodule Phoenix.Socket.Server do\n+  @moduledoc \"\"\"\n+  Defines a server for socket operations. Delegates to the actual Transport.\n+  \"\"\"\n+  use GenServer\n+  require Logger\n+  alias Phoenix.Socket\n+  alias Phoenix.Socket.Message\n+  alias Phoenix.PubSub\n+\n+  # External API\n+\n+  def start_link(%Socket{} = socket) do\n+    GenServer.start_link(__MODULE__, socket)\n+  end\n+\n+  def dispatch_join(server, topic, message) do\n+    GenServer.call(server, {:dispatch_join, topic, message})\n+  end\n+\n+  def dispatch_in(server, topic, event, message) do\n+    GenServer.call(server, {:dispatch_in, topic, event, message})\n+  end\n+\n+  def dispatch_out(server, event, message) do\n+    GenServer.call(server, {:dispatch_out, event, message})\n+  end\n+\n+  def dispatch_leave(server, message) do\n+    GenServer.call(server, {:dispatch_leave, :ignore_topic, message})\n+  end\n+  def dispatch_leave(server, topic, message) do\n+    GenServer.call(server, {:dispatch_leave, topic, message})\n+  end\n+\n+  def do_join(server) do\n+    GenServer.call(server, :do_join)\n+  end\n+\n+  def do_leave(server) do\n+    GenServer.call(server, :do_leave)\n+  end\n+\n+  def authorized?(server, topic) do\n+    GenServer.call(server, {:authorized, topic})\n+  end\n+\n+  def dispatch_reply(server, %Message{} = message) do\n+    GenServer.cast(server, {:dispatch_reply, message})\n+  end\n+\n+  # Server internal\n+\n+  def init(%Socket{} = socket) do\n+    {:ok, socket}\n+  end\n+\n+  def handle_call({:dispatch_join, topic, message}, _from, socket) do\n+    case socket.router.channel_for_topic(topic, socket.transport) do\n+      nil ->\n+        Logger.debug fn -> \"Ignoring unmatched topic \\\"#{socket.topic}\\\" in #{inspect(socket.router)}\" end\n+        create_reply(:ignore)\n+      channel ->\n+        topic\n+        |> channel.join(message, Socket.put_channel(socket, channel))\n+        |> create_reply\n+    end\n+  end\n+\n+  def handle_call({:dispatch_in, topic, event, message}, _from, socket) do\n+    if Socket.authorized?(socket, topic) do\n+      socket.channel.handle_in(event, message, socket)\n+      |> create_reply\n+    else\n+      create_reply({:error, :unauthenticated, socket})\n+    end\n+  end\n+\n+  def handle_call({:dispatch_out, event, message}, _from, socket) do\n+    socket.channel.handle_out(event, message, socket)\n+    |> create_reply\n+  end\n+\n+  def handle_call({:dispatch_leave, :ignore_topic, message}, _from, socket) do\n+    socket.channel.leave(message, socket)\n+    |> create_reply\n+  end\n+\n+  def handle_call({:dispatch_leave, topic, message}, _from, socket) do\n+    if Socket.authorized?(socket, topic) do\n+      socket.channel.leave(message, socket)\n+      |> create_reply\n+    else\n+      create_reply({:error, :unauthenticated, socket})\n+    end\n+  end\n+\n+  def handle_call(:do_join, _from, socket) do\n+    PubSub.subscribe(socket.pubsub_server, socket.pid, socket.topic, link: true)\n+    {:reply, :ok, Socket.authorize(socket, socket.topic)}\n+  end\n+\n+  def handle_call(:do_leave, _from, socket) do\n+    PubSub.unsubscribe(socket.pubsub_server, socket.pid, socket.topic)\n+    {:reply, :ok, Socket.deauthorize(socket)}\n+  end\n+\n+  def handle_call({:authorized, topic}, _from, socket) do\n+    {:reply, Socket.authorized?(socket, topic), socket}\n+  end\n+\n+  def handle_cast({:dispatch_reply, message}, socket) do\n+    send socket.pid, {:socket_reply, message}\n+    {:noreply, socket}\n+  end\n+\n+  def handle_info(message, socket) do\n+    {:ok, socket} = socket.channel.handle_info(message, socket)\n+    # TODO accept the normal channel results here?\n+    {:noreply, socket}\n+  end\n+","path":"lib/phoenix/socket/server.ex","position":148,"original_position":148,"commit_id":"93f01bc03734d21a84aaaa24b3d2d02001a4daf8","original_commit_id":"93f01bc03734d21a84aaaa24b3d2d02001a4daf8","user":{"login":"josevalim","id":9582,"avatar_url":"https://avatars.githubusercontent.com/u/9582?v=3","gravatar_id":"","url":"https://api.github.com/users/josevalim","html_url":"https://github.com/josevalim","followers_url":"https://api.github.com/users/josevalim/followers","following_url":"https://api.github.com/users/josevalim/following{/other_user}","gists_url":"https://api.github.com/users/josevalim/gists{/gist_id}","starred_url":"https://api.github.com/users/josevalim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josevalim/subscriptions","organizations_url":"https://api.github.com/users/josevalim/orgs","repos_url":"https://api.github.com/users/josevalim/repos","events_url":"https://api.github.com/users/josevalim/events{/privacy}","received_events_url":"https://api.github.com/users/josevalim/received_events","type":"User","site_admin":false},"body":"We should likely call leave in `terminate` too.","created_at":"2015-03-03T06:28:23Z","updated_at":"2015-03-03T06:28:23Z","html_url":"https://github.com/phoenixframework/phoenix/pull/648#discussion_r25665731","pull_request_url":"https://api.github.com/repos/phoenixframework/phoenix/pulls/648","_links":{"self":{"href":"https://api.github.com/repos/phoenixframework/phoenix/pulls/comments/25665731"},"html":{"href":"https://github.com/phoenixframework/phoenix/pull/648#discussion_r25665731"},"pull_request":{"href":"https://api.github.com/repos/phoenixframework/phoenix/pulls/648"}}},"pull_request":{"url":"https://api.github.com/repos/phoenixframework/phoenix/pulls/648","id":30324768,"html_url":"https://github.com/phoenixframework/phoenix/pull/648","diff_url":"https://github.com/phoenixframework/phoenix/pull/648.diff","patch_url":"https://github.com/phoenixframework/phoenix/pull/648.patch","issue_url":"https://api.github.com/repos/phoenixframework/phoenix/issues/648","number":648,"state":"open","locked":false,"title":"Adds support for processes per Phoenix.Socket","user":{"login":"ephe-meral","id":1501017,"avatar_url":"https://avatars.githubusercontent.com/u/1501017?v=3","gravatar_id":"","url":"https://api.github.com/users/ephe-meral","html_url":"https://github.com/ephe-meral","followers_url":"https://api.github.com/users/ephe-meral/followers","following_url":"https://api.github.com/users/ephe-meral/following{/other_user}","gists_url":"https://api.github.com/users/ephe-meral/gists{/gist_id}","starred_url":"https://api.github.com/users/ephe-meral/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ephe-meral/subscriptions","organizations_url":"https://api.github.com/users/ephe-meral/orgs","repos_url":"https://api.github.com/users/ephe-meral/repos","events_url":"https://api.github.com/users/ephe-meral/events{/privacy}","received_events_url":"https://api.github.com/users/ephe-meral/received_events","type":"User","site_admin":false},"body":"RFC\r\n\r\nMain goal of these changes was to implement `handle_info` for channels, so that the user can send arbitrary messages to its own socket-process (e.g. from within the channel) and handle them there.\r\n\r\nMight break backwards compatibility with code that relies on the inner workings of Transport. ","created_at":"2015-03-02T21:56:40Z","updated_at":"2015-03-03T06:28:23Z","closed_at":null,"merged_at":null,"merge_commit_sha":"977ba7171d003e440ba1b4319bd1e9ec4f662f37","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/phoenixframework/phoenix/pulls/648/commits","review_comments_url":"https://api.github.com/repos/phoenixframework/phoenix/pulls/648/comments","review_comment_url":"https://api.github.com/repos/phoenixframework/phoenix/pulls/comments{/number}","comments_url":"https://api.github.com/repos/phoenixframework/phoenix/issues/648/comments","statuses_url":"https://api.github.com/repos/phoenixframework/phoenix/statuses/93f01bc03734d21a84aaaa24b3d2d02001a4daf8","head":{"label":"ephe-meral:master","ref":"master","sha":"93f01bc03734d21a84aaaa24b3d2d02001a4daf8","user":{"login":"ephe-meral","id":1501017,"avatar_url":"https://avatars.githubusercontent.com/u/1501017?v=3","gravatar_id":"","url":"https://api.github.com/users/ephe-meral","html_url":"https://github.com/ephe-meral","followers_url":"https://api.github.com/users/ephe-meral/followers","following_url":"https://api.github.com/users/ephe-meral/following{/other_user}","gists_url":"https://api.github.com/users/ephe-meral/gists{/gist_id}","starred_url":"https://api.github.com/users/ephe-meral/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ephe-meral/subscriptions","organizations_url":"https://api.github.com/users/ephe-meral/orgs","repos_url":"https://api.github.com/users/ephe-meral/repos","events_url":"https://api.github.com/users/ephe-meral/events{/privacy}","received_events_url":"https://api.github.com/users/ephe-meral/received_events","type":"User","site_admin":false},"repo":{"id":28741432,"name":"phoenix","full_name":"ephe-meral/phoenix","owner":{"login":"ephe-meral","id":1501017,"avatar_url":"https://avatars.githubusercontent.com/u/1501017?v=3","gravatar_id":"","url":"https://api.github.com/users/ephe-meral","html_url":"https://github.com/ephe-meral","followers_url":"https://api.github.com/users/ephe-meral/followers","following_url":"https://api.github.com/users/ephe-meral/following{/other_user}","gists_url":"https://api.github.com/users/ephe-meral/gists{/gist_id}","starred_url":"https://api.github.com/users/ephe-meral/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ephe-meral/subscriptions","organizations_url":"https://api.github.com/users/ephe-meral/orgs","repos_url":"https://api.github.com/users/ephe-meral/repos","events_url":"https://api.github.com/users/ephe-meral/events{/privacy}","received_events_url":"https://api.github.com/users/ephe-meral/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ephe-meral/phoenix","description":"Elixir Web Framework","fork":true,"url":"https://api.github.com/repos/ephe-meral/phoenix","forks_url":"https://api.github.com/repos/ephe-meral/phoenix/forks","keys_url":"https://api.github.com/repos/ephe-meral/phoenix/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ephe-meral/phoenix/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ephe-meral/phoenix/teams","hooks_url":"https://api.github.com/repos/ephe-meral/phoenix/hooks","issue_events_url":"https://api.github.com/repos/ephe-meral/phoenix/issues/events{/number}","events_url":"https://api.github.com/repos/ephe-meral/phoenix/events","assignees_url":"https://api.github.com/repos/ephe-meral/phoenix/assignees{/user}","branches_url":"https://api.github.com/repos/ephe-meral/phoenix/branches{/branch}","tags_url":"https://api.github.com/repos/ephe-meral/phoenix/tags","blobs_url":"https://api.github.com/repos/ephe-meral/phoenix/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ephe-meral/phoenix/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ephe-meral/phoenix/git/refs{/sha}","trees_url":"https://api.github.com/repos/ephe-meral/phoenix/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ephe-meral/phoenix/statuses/{sha}","languages_url":"https://api.github.com/repos/ephe-meral/phoenix/languages","stargazers_url":"https://api.github.com/repos/ephe-meral/phoenix/stargazers","contributors_url":"https://api.github.com/repos/ephe-meral/phoenix/contributors","subscribers_url":"https://api.github.com/repos/ephe-meral/phoenix/subscribers","subscription_url":"https://api.github.com/repos/ephe-meral/phoenix/subscription","commits_url":"https://api.github.com/repos/ephe-meral/phoenix/commits{/sha}","git_commits_url":"https://api.github.com/repos/ephe-meral/phoenix/git/commits{/sha}","comments_url":"https://api.github.com/repos/ephe-meral/phoenix/comments{/number}","issue_comment_url":"https://api.github.com/repos/ephe-meral/phoenix/issues/comments{/number}","contents_url":"https://api.github.com/repos/ephe-meral/phoenix/contents/{+path}","compare_url":"https://api.github.com/repos/ephe-meral/phoenix/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ephe-meral/phoenix/merges","archive_url":"https://api.github.com/repos/ephe-meral/phoenix/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ephe-meral/phoenix/downloads","issues_url":"https://api.github.com/repos/ephe-meral/phoenix/issues{/number}","pulls_url":"https://api.github.com/repos/ephe-meral/phoenix/pulls{/number}","milestones_url":"https://api.github.com/repos/ephe-meral/phoenix/milestones{/number}","notifications_url":"https://api.github.com/repos/ephe-meral/phoenix/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ephe-meral/phoenix/labels{/name}","releases_url":"https://api.github.com/repos/ephe-meral/phoenix/releases{/id}","created_at":"2015-01-03T10:58:19Z","updated_at":"2015-03-02T21:51:37Z","pushed_at":"2015-03-02T21:51:36Z","git_url":"git://github.com/ephe-meral/phoenix.git","ssh_url":"git@github.com:ephe-meral/phoenix.git","clone_url":"https://github.com/ephe-meral/phoenix.git","svn_url":"https://github.com/ephe-meral/phoenix","homepage":"","size":2154,"stargazers_count":0,"watchers_count":0,"language":"Elixir","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"phoenixframework:master","ref":"master","sha":"bbaa6d0abbb269115eef2ee6f59ef9c835a23681","user":{"login":"phoenixframework","id":6510388,"avatar_url":"https://avatars.githubusercontent.com/u/6510388?v=3","gravatar_id":"","url":"https://api.github.com/users/phoenixframework","html_url":"https://github.com/phoenixframework","followers_url":"https://api.github.com/users/phoenixframework/followers","following_url":"https://api.github.com/users/phoenixframework/following{/other_user}","gists_url":"https://api.github.com/users/phoenixframework/gists{/gist_id}","starred_url":"https://api.github.com/users/phoenixframework/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/phoenixframework/subscriptions","organizations_url":"https://api.github.com/users/phoenixframework/orgs","repos_url":"https://api.github.com/users/phoenixframework/repos","events_url":"https://api.github.com/users/phoenixframework/events{/privacy}","received_events_url":"https://api.github.com/users/phoenixframework/received_events","type":"Organization","site_admin":false},"repo":{"id":16072585,"name":"phoenix","full_name":"phoenixframework/phoenix","owner":{"login":"phoenixframework","id":6510388,"avatar_url":"https://avatars.githubusercontent.com/u/6510388?v=3","gravatar_id":"","url":"https://api.github.com/users/phoenixframework","html_url":"https://github.com/phoenixframework","followers_url":"https://api.github.com/users/phoenixframework/followers","following_url":"https://api.github.com/users/phoenixframework/following{/other_user}","gists_url":"https://api.github.com/users/phoenixframework/gists{/gist_id}","starred_url":"https://api.github.com/users/phoenixframework/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/phoenixframework/subscriptions","organizations_url":"https://api.github.com/users/phoenixframework/orgs","repos_url":"https://api.github.com/users/phoenixframework/repos","events_url":"https://api.github.com/users/phoenixframework/events{/privacy}","received_events_url":"https://api.github.com/users/phoenixframework/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/phoenixframework/phoenix","description":"A highly connected web framework for the Elixir programming language","fork":false,"url":"https://api.github.com/repos/phoenixframework/phoenix","forks_url":"https://api.github.com/repos/phoenixframework/phoenix/forks","keys_url":"https://api.github.com/repos/phoenixframework/phoenix/keys{/key_id}","collaborators_url":"https://api.github.com/repos/phoenixframework/phoenix/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/phoenixframework/phoenix/teams","hooks_url":"https://api.github.com/repos/phoenixframework/phoenix/hooks","issue_events_url":"https://api.github.com/repos/phoenixframework/phoenix/issues/events{/number}","events_url":"https://api.github.com/repos/phoenixframework/phoenix/events","assignees_url":"https://api.github.com/repos/phoenixframework/phoenix/assignees{/user}","branches_url":"https://api.github.com/repos/phoenixframework/phoenix/branches{/branch}","tags_url":"https://api.github.com/repos/phoenixframework/phoenix/tags","blobs_url":"https://api.github.com/repos/phoenixframework/phoenix/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/phoenixframework/phoenix/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/phoenixframework/phoenix/git/refs{/sha}","trees_url":"https://api.github.com/repos/phoenixframework/phoenix/git/trees{/sha}","statuses_url":"https://api.github.com/repos/phoenixframework/phoenix/statuses/{sha}","languages_url":"https://api.github.com/repos/phoenixframework/phoenix/languages","stargazers_url":"https://api.github.com/repos/phoenixframework/phoenix/stargazers","contributors_url":"https://api.github.com/repos/phoenixframework/phoenix/contributors","subscribers_url":"https://api.github.com/repos/phoenixframework/phoenix/subscribers","subscription_url":"https://api.github.com/repos/phoenixframework/phoenix/subscription","commits_url":"https://api.github.com/repos/phoenixframework/phoenix/commits{/sha}","git_commits_url":"https://api.github.com/repos/phoenixframework/phoenix/git/commits{/sha}","comments_url":"https://api.github.com/repos/phoenixframework/phoenix/comments{/number}","issue_comment_url":"https://api.github.com/repos/phoenixframework/phoenix/issues/comments{/number}","contents_url":"https://api.github.com/repos/phoenixframework/phoenix/contents/{+path}","compare_url":"https://api.github.com/repos/phoenixframework/phoenix/compare/{base}...{head}","merges_url":"https://api.github.com/repos/phoenixframework/phoenix/merges","archive_url":"https://api.github.com/repos/phoenixframework/phoenix/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/phoenixframework/phoenix/downloads","issues_url":"https://api.github.com/repos/phoenixframework/phoenix/issues{/number}","pulls_url":"https://api.github.com/repos/phoenixframework/phoenix/pulls{/number}","milestones_url":"https://api.github.com/repos/phoenixframework/phoenix/milestones{/number}","notifications_url":"https://api.github.com/repos/phoenixframework/phoenix/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/phoenixframework/phoenix/labels{/name}","releases_url":"https://api.github.com/repos/phoenixframework/phoenix/releases{/id}","created_at":"2014-01-20T14:14:11Z","updated_at":"2015-03-03T02:21:29Z","pushed_at":"2015-03-03T02:31:29Z","git_url":"git://github.com/phoenixframework/phoenix.git","ssh_url":"git@github.com:phoenixframework/phoenix.git","clone_url":"https://github.com/phoenixframework/phoenix.git","svn_url":"https://github.com/phoenixframework/phoenix","homepage":"http://www.phoenixframework.org","size":7320,"stargazers_count":2009,"watchers_count":2009,"language":"Elixir","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":194,"mirror_url":null,"open_issues_count":20,"forks":194,"open_issues":20,"watchers":2009,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/phoenixframework/phoenix/pulls/648"},"html":{"href":"https://github.com/phoenixframework/phoenix/pull/648"},"issue":{"href":"https://api.github.com/repos/phoenixframework/phoenix/issues/648"},"comments":{"href":"https://api.github.com/repos/phoenixframework/phoenix/issues/648/comments"},"review_comments":{"href":"https://api.github.com/repos/phoenixframework/phoenix/pulls/648/comments"},"review_comment":{"href":"https://api.github.com/repos/phoenixframework/phoenix/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/phoenixframework/phoenix/pulls/648/commits"},"statuses":{"href":"https://api.github.com/repos/phoenixframework/phoenix/statuses/93f01bc03734d21a84aaaa24b3d2d02001a4daf8"}}}}