{"id":13508727,"url":"https://github.com/meh/elixir-socket","last_synced_at":"2025-05-14T23:07:04.864Z","repository":{"id":8321058,"uuid":"9871372","full_name":"meh/elixir-socket","owner":"meh","description":"Socket wrapping for Elixir.","archived":false,"fork":false,"pushed_at":"2023-02-22T15:16:32.000Z","size":276,"stargazers_count":691,"open_issues_count":32,"forks_count":117,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-04-30T00:24:48.205Z","etag":null,"topics":["elixir","socket","ssl","tcp","udp","websocket"],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/meh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2013-05-05T16:41:40.000Z","updated_at":"2025-03-22T07:10:48.000Z","dependencies_parsed_at":"2024-01-08T19:22:16.180Z","dependency_job_id":null,"html_url":"https://github.com/meh/elixir-socket","commit_stats":{"total_commits":294,"total_committers":39,"mean_commits":7.538461538461538,"dds":"0.15646258503401356","last_synced_commit":"22a65b2739611bf1f00135b72a21b256a16f645f"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meh%2Felixir-socket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meh%2Felixir-socket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meh%2Felixir-socket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meh%2Felixir-socket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/meh","download_url":"https://codeload.github.com/meh/elixir-socket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252417778,"owners_count":21744677,"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","socket","ssl","tcp","udp","websocket"],"created_at":"2024-08-01T02:00:57.588Z","updated_at":"2025-05-14T23:06:59.840Z","avatar_url":"https://github.com/meh.png","language":"Elixir","readme":"Elixir sockets made decent\n==========================\nThis library wraps `gen_tcp`, `gen_udp` and `gen_sctp`, `ssl` and implements\nwebsockets and socks.\n\nInstallation\n--------\nIn your `mix.exs` file\n\n```\ndefp deps do\n  [\n    # ...\n    {:socket, \"~\u003e 0.3\"},\n    # ...\n  ]\nend\n```\n\nThen run `mix deps.get` to install\n\nExamples\n--------\n\n```elixir\ndefmodule HTTP do\n  def get(uri) when is_binary(uri) or is_list(uri) do\n    get(URI.parse(uri))\n  end\n\n  def get(%URI{host: host, port: port, path: path}) do\n    sock = Socket.TCP.connect! host, port, packet: :line\n    sock |\u003e Socket.Stream.send! \"GET #{path || \"/\"} HTTP/1.1\\r\\nHost: #{host}\\r\\n\\r\\n\"\n\n    [_, code, text] = Regex.run ~r\"HTTP/1.1 (.*?) (.*?)\\s*$\", sock |\u003e Socket.Stream.recv!\n\n    headers = headers([], sock) |\u003e Enum.into(%{})\n\n    sock |\u003e Socket.packet! :raw\n    body = sock |\u003e Socket.Stream.recv!(String.to_integer(headers[\"Content-Length\"]))\n\n    { { String.to_integer(code), text }, headers, body }\n  end\n\n  defp headers(acc, sock) do\n    case sock |\u003e Socket.Stream.recv! do\n      \"\\r\\n\" -\u003e\n        acc\n\n      line -\u003e\n        [_, name, value] = Regex.run ~r/^(.*?):\\s*(.*?)\\s*$/, line\n\n        headers([{ name, value } | acc], sock)\n    end\n  end\nend\n```\n\nWebsockets\n----------\n\n### Client\n\n```elixir\nsocket = Socket.Web.connect! \"echo.websocket.org\"\nsocket |\u003e Socket.Web.send! { :text, \"test\" }\nsocket |\u003e Socket.Web.recv! # =\u003e {:text, \"test\"}\n```\n\nIn order to connect to a TLS websocket, use the `secure: true` option:\n\n```elixir\nsocket = Socket.Web.connect! \"echo.websocket.org\", secure: true\n```\n\nThe `connect!` function also accepts other parameters, most notably the `path` parameter, which is used when the websocket server endpoint exists on a path below the domain ie. \"example.com/websocket\":\n\n```elixir\nsocket = Socket.Web.connect! \"example.com\", path: \"/websocket\"\n```\n\nNote that websocket servers send ping messages. A pong reply from your client tells the server to keep the connection open and to send more data. If your client doesn't send a pong reply then the server will close the connection. Here's an example of how to get get both the data you want and reply to a server's pings:\n\n```elixir\nsocket = Socket.Web.connect! \"echo.websocket.org\"\ncase socket |\u003e Socket.Web.recv! do\n  {:text, data} -\u003e\n    # process data\n  {:ping, _ } -\u003e\n    socket |\u003e Socket.Web.send!({:pong, \"\"})\nend\n```\n\n### Server\n\n```elixir\nserver = Socket.Web.listen! 80\nclient = server |\u003e Socket.Web.accept!\n\n# here you can verify if you want to accept the request or not, call\n# `Socket.Web.close!` if you don't want to accept it, or else call\n# `Socket.Web.accept!`\nclient |\u003e Socket.Web.accept!\n\n# echo the first message\nclient |\u003e Socket.Web.send!(client |\u003e Socket.Web.recv!)\n```\n","funding_links":[],"categories":["Networking","Elixir"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmeh%2Felixir-socket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmeh%2Felixir-socket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmeh%2Felixir-socket/lists"}