{"id":19839256,"url":"https://github.com/gsmlg-dev/phoenix_session_process","last_synced_at":"2025-05-01T18:31:55.198Z","repository":{"id":220918771,"uuid":"752890970","full_name":"gsmlg-dev/phoenix_session_process","owner":"gsmlg-dev","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-15T14:07:21.000Z","size":35,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T15:15:44.972Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gsmlg-dev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2024-02-05T03:27:37.000Z","updated_at":"2025-02-15T14:05:54.000Z","dependencies_parsed_at":"2024-02-05T06:50:09.359Z","dependency_job_id":"988158ba-2100-433e-8fff-6b2eaf79553f","html_url":"https://github.com/gsmlg-dev/phoenix_session_process","commit_stats":null,"previous_names":["gsmlg-dev/phoenix_session_process"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsmlg-dev%2Fphoenix_session_process","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsmlg-dev%2Fphoenix_session_process/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsmlg-dev%2Fphoenix_session_process/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsmlg-dev%2Fphoenix_session_process/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gsmlg-dev","download_url":"https://codeload.github.com/gsmlg-dev/phoenix_session_process/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251924831,"owners_count":21666043,"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":[],"created_at":"2024-11-12T12:21:22.411Z","updated_at":"2025-05-01T18:31:54.895Z","avatar_url":"https://github.com/gsmlg-dev.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Phoenix.SessionProcess\n\nCreate a process for each session, all user requests would through this process.\n\n* [Github Repo](https://github.com/gsmlg-dev/phoenix_session_process)\n\n## Installation\n\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed\nby adding `phoenix_session_process` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:phoenix_session_process, \"~\u003e 0.3.1\"}\n  ]\nend\n```\n\nDocumentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)\nand published on [HexDocs](https://hexdocs.pm). Once published, the docs can\nbe found at \u003chttps://hexdocs.pm/phoenix_session_process\u003e.\n\n\n## How to\n\nAdd superviser to process tree\n\n```elixir\n    [\n      ...\n      {Phoenix.SessionProcess.Supervisor, []}\n    ]\n```\n\nAdd this after the `:fetch_session` plug to generate a unique session ID.\n\n```elixir\n    plug :fetch_session\n    plug Phoenix.SessionProcess.SessionId\n```\n\nStart a session process with a session ID.\n\n```elixir\n    Phoenix.SessionProcess.start(\"session_id\")\n```\n\nThis will start a session process using the module defined with\n\n```elixir\n    config :phoenix_session_process, session_process: MySessionProcess\n```\n\nDefine MySessionProcess\n\n```elixir\ndefmodule MySessionProcess do\n  @doc \"\"\"\n  The use macro is expanded as\n\n      use GenServer\n\n      def start_link(name: name, arg: arg) do\n        GenServer.start_link(__MODULE__, arg, name: name)\n      end\n      def start_link(name: name) do\n        GenServer.start_link(__MODULE__, %{}, name: name)\n      end\n  \"\"\"\n  use Phoenix.SessionProcess, :process\nend\n\n# with monitor live view\ndefmodule MySessionProcessWithMonitor do\n  @doc \"\"\"\n  The use macro is expanded as\n\n      use GenServer\n\n      def start_link(name: name, arg: arg) do\n        GenServer.start_link(__MODULE__, arg, name: name)\n      end\n      def start_link(name: name) do\n        GenServer.start_link(__MODULE__, %{}, name: name)\n      end\n\n      @impl true\n      def init(arg) do\n        Process.flag(:trap_exit, true)\n\n        {:ok, state}\n      end\n\n      @impl true\n      def handle_call(:get_state, _from, state) do\n        {:reply, state, state}\n      end\n\n      @impl true\n      def handle_cast({:monitor, pid}, state) do\n        state = state |\u003e Map.update(:__live_view__, [pid], fn views -\u003e [pid | views] end)\n        Process.monitor(pid)\n        {:noreply, state}\n      end\n\n      @impl true\n      def handle_info({:DOWN, _ref, :process, pid, _reason}, state) do\n        state = state |\u003e Map.update(:__live_view__, [], fn views -\u003e views |\u003e Enum.filter(\u0026(\u00261 != pid)) end)\n\n        {:noreply, state}\n      end\n\n      @impl true\n      def terminate(reason, state) do\n        state\n        |\u003e Map.get(:__live_view__, [])\n        |\u003e Enum.each(\u0026Process.send_after(\u00261, :session_expired, 0))\n      end\n\n  In live view\n\n      def mount(_params, %{\"session_id\" =\u003e session_id} = _session, socket) do\n        socket = socket\n        |\u003e assign(:session_id, session_id)\n\n        Phoenix.SessionProcess.cast(session_id, {:monitor, self()})\n\n        {:ok, socket}\n      end\n\n      # trap session expires\n      def handle_info(:session_expired, socket) do\n        {:noreply, socket |\u003e redirect(to: @sign_in_page)}\n      end\n  \"\"\"\n  use Phoenix.SessionProcess, :process\nend\n```\n\nOr you can start a session process with a specific module.\n\n```elixir\n    Phoenix.SessionProcess.start(\"session_id\", MySessionProcess)\n    # or\n    Phoenix.SessionProcess.start(\"session_id\", MySessionProcess, arg)\n```\n\nCheck if a session process is started.\n\n```elixir\n    Phoenix.SessionProcess.started?(\"session_id\")\n```\n\nTerminate a session process.\n\n```elixir\n    Phoenix.SessionProcess.terminate(\"session_id\")\n```\n\nGenserver call on a session process.\n\n```elixir\n    Phoenix.SessionProcess.call(\"session_id\", request)\n```\n\nGenserver cast on a session process.\n\n```elixir\n    Phoenix.SessionProcess.cast(\"session_id\", request)\n```\n\nGet session id in SessionProcess.\n\n```elixir\n    get_session_id()\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgsmlg-dev%2Fphoenix_session_process","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgsmlg-dev%2Fphoenix_session_process","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgsmlg-dev%2Fphoenix_session_process/lists"}