{"id":13508346,"url":"https://github.com/slogsdon/plug-web-socket","last_synced_at":"2025-04-13T00:34:32.024Z","repository":{"id":31326239,"uuid":"34888760","full_name":"slogsdon/plug-web-socket","owner":"slogsdon","description":"An exploration into a stand-alone library for Plug applications to easily adopt WebSockets.","archived":false,"fork":false,"pushed_at":"2017-07-14T01:59:52.000Z","size":33,"stargazers_count":63,"open_issues_count":1,"forks_count":17,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-13T00:34:07.397Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/slogsdon.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}},"created_at":"2015-05-01T03:52:47.000Z","updated_at":"2024-10-12T18:25:18.000Z","dependencies_parsed_at":"2022-09-09T07:30:58.105Z","dependency_job_id":null,"html_url":"https://github.com/slogsdon/plug-web-socket","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slogsdon%2Fplug-web-socket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slogsdon%2Fplug-web-socket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slogsdon%2Fplug-web-socket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slogsdon%2Fplug-web-socket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slogsdon","download_url":"https://codeload.github.com/slogsdon/plug-web-socket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650488,"owners_count":21139670,"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-08-01T02:00:51.789Z","updated_at":"2025-04-13T00:34:32.004Z","avatar_url":"https://github.com/slogsdon.png","language":"Elixir","funding_links":[],"categories":["HTTP"],"sub_categories":[],"readme":"# WebSocket\n[![Build Status](https://img.shields.io/travis/slogsdon/plug-web-socket.svg?style=flat)](https://travis-ci.org/slogsdon/plug-web-socket)\n[![Coverage Status](https://img.shields.io/coveralls/slogsdon/plug-web-socket.svg?style=flat)](https://coveralls.io/r/slogsdon/plug-web-socket)\n[![Hex.pm Version](http://img.shields.io/hexpm/v/web_socket.svg?style=flat)](https://hex.pm/packages/web_socket)\n\nAn exploration into a stand-alone library for\nPlug applications to easily adopt WebSockets.\n\n## Viewing the examples\n\nRun these:\n\n```\n$ git clone https://github.com/slogsdon/plug-web-socket\n$ cd plug-web-socket\n$ mix deps.get\n$ iex -S mix run run_examples.exs\n```\n\nGo here: \u003chttp://localhost:4000\u003e.\n\nYou will be presented with a list of possible\nexamples/tests that use a WebSocket connection.\n\n## Integrating with Plug\n\nIf you're looking to try this in your own test\napplication, do something like this:\n\n```elixir\ndefmodule MyApp.Router do\n  use Plug.Router\n  use WebSocket\n\n  # WebSocket routes\n  #      route     controller/handler     function \u0026 name\n  socket \"/topic\", MyApp.TopicController, :handle\n  socket \"/echo\",  MyApp.EchoController,  :echo\n\n  # Rest of your router's plugs and routes\n  # ...\n\n  def run(opts \\\\ []) do\n    dispatch = dispatch_table(opts)\n    Plug.Adapters.Cowboy.http __MODULE__, opts, [dispatch: dispatch]\n  end\nend\n```\n\nFor the time being, there is a `run/1` function\ngenerated for your router that starts a HTTP/WS\nlistener. Not sure if this will stay or get\nreduced to helper functions that aid in the\ncreation of a similar function. Most likely the\nlatter will win out to help compose functionality.\nThe big part that it plays is the building of a\ndispatch table to pass as an option to Cowboy that\nhas an entry for each of your socket routes and a\ncatch all for HTTP requests.\n\n### Add the necessary bits to a module\n\nFrom the topic example:\n\n```elixir\ndefmodule MyApp.TopicController do\n  def handle(:init, state) do\n    {:ok, state}\n  end\n  def handle(:terminate, _state) do\n    :ok\n  end\n  def handle(\"topic:\" \u003c\u003e letter, state, data) do\n    payload = %{awesome: \"blah #{letter}\",\n                orig: data}\n    {:reply, {:text, payload}, state}\n  end\nend\n```\n\nCurrently, the function name needs to be unique\nacross all controllers/handlers as its used for\nthe Events layer.\n\n### Broadcast from elsewhere\n\nNeed to send data out from elsewhere in your app?\n\n```elixir\n# Build your message\ntopic = \"my_event\"\ndata  = %{foo: \"awesome\"}\nmes   = WebSocket.Message.build(topic, data)\njson  = Poison.encode!(mes)\n\n# Pick your destination (from your routes)\nname = :handle\n\n# Send away!\nWebSockets.broadcast!(name, json)\n```\n\nThis needs to be nicer, but this is still in\nprogress.\n\n## License\n\nWebSocket is released under the MIT License.\n\nSee [LICENSE](https://github.com/slogsdon/plug-web-socket/blob/master/LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslogsdon%2Fplug-web-socket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslogsdon%2Fplug-web-socket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslogsdon%2Fplug-web-socket/lists"}