{"id":13576310,"url":"https://github.com/doorgan/channel_handler","last_synced_at":"2025-04-12T19:49:57.594Z","repository":{"id":95269912,"uuid":"589152185","full_name":"doorgan/channel_handler","owner":"doorgan","description":"A declarative DSL to work with Phoenix Channels","archived":false,"fork":false,"pushed_at":"2024-11-14T22:22:06.000Z","size":100,"stargazers_count":106,"open_issues_count":1,"forks_count":8,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-12T19:49:48.952Z","etag":null,"topics":["elixir","elixir-lang","elixir-phoenix","phoenix","phoenix-channels","phoenix-framework"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/doorgan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-15T08:34:02.000Z","updated_at":"2025-04-09T19:40:41.000Z","dependencies_parsed_at":"2024-12-29T19:21:09.428Z","dependency_job_id":"413521e9-13a0-4819-ae26-e78609d64738","html_url":"https://github.com/doorgan/channel_handler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doorgan%2Fchannel_handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doorgan%2Fchannel_handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doorgan%2Fchannel_handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doorgan%2Fchannel_handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/doorgan","download_url":"https://codeload.github.com/doorgan/channel_handler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625501,"owners_count":21135513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","elixir-lang","elixir-phoenix","phoenix","phoenix-channels","phoenix-framework"],"created_at":"2024-08-01T15:01:09.073Z","updated_at":"2025-04-12T19:49:57.555Z","avatar_url":"https://github.com/doorgan.png","language":"Elixir","readme":"# ChannelHandler\n\nHelpers to organize and route messages in complex Phoenix channels.\n\n## Installation\n\nAdd the following to your mix.exs dependencies:\n\n```elixir\n{:channel_handler, \"~\u003e 0.6\"}\n```\n\n## Usage\n\nAfter defining a channel with vanilla Phoenix's `channel`:\n\n```elixir\nchannel \"post:*\", MyAppWeb.PostChannel\n```\n\nDefine your channel handler, and add `use ChannelHandler.Router` in addition to the\n`use MyAppWeb, :channel`:\n\n```elixir\ndefmodule MyAppWeb.PostChannel do\n  use MyAppWeb, :channel\n  use ChannelHandler.Router\nend\n```\n\nNow you can start defining matchers and event handlers:\n\n```elixir\ndefmodule MyAppWeb.PostChannel do\n  use MyAppWeb, :channel\n  use ChannelHandler.Router\n\n  join fn _topic, _payload, socket -\u003e\n    {:ok, socket}\n  end\n\n  plug MyAppWeb.ChannelPlugs.EnsureAuthenticated\n\n  event \"comments:create\", MyAppWeb.PostCommentsHandler, :create\n\n  delegate \"comments:\", MyAppWeb.PostCommentsHandler\n\n  handle \"post:create\", fn payload, _bindings, socket -\u003e\n    case MyApp.Posts.create(payload) do\n      {:ok, post} -\u003e\n        {:reply, {:ok, post}, socket}\n\n      {:error, reason} -\u003e\n        {:reply, {:error, reason}, socket}\n    end\n  end\n\n  scope \"secret:\" do\n    plug \u0026check_permission/4, :do_secret_stuff\n\n    # You can use splat patterns to match anything with *\n    event \"foo:*\", SuperSecretHandler, :foo\n\n    # Not specifying a prefix matches everything\n    delegate SuperSecretHandler\n  end\n\n  def check_permission(socket, _payload, _bindings, permission) do\n    if MyApp.Authorization.can?(socket.assigns.current_user, permission) do\n      # using :cont resumes the event handling\n      {:cont, socket, payload, bindings}\n    else\n      # returning :reply or :noreply halts the event handling\n      {:reply, {:error, \"Unauthorized\"}, socket}\n    end\n  end\nend\n\ndefmodule MyAppWeb.PostCommentsHandler do\n  use ChannelHandler.Handler\n\n  # Add a plug only for the create action\n  plug MyAppWeb.ChannelPlugs.CheckPermission, :comment_posts when action in [:create]\n  \n  def handle_in(event, payload, bindings, socket) do\n    # Do something with the delegated event\n  end\n\n  def create(payload, bindings, socket) do\n    # Create a comment\n  end\nend\n```\n\nFor more explanations about the API functions and what's possible, check the\ndocs for `ChannelHandler.Dsl`.\n\n## Copyright and License\nCopyright (c) 2023 dorgandash@gmail.com\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n","funding_links":[],"categories":["Elixir"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoorgan%2Fchannel_handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdoorgan%2Fchannel_handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoorgan%2Fchannel_handler/lists"}