{"id":13705925,"url":"https://github.com/rstudio/httpuv","last_synced_at":"2025-05-14T08:09:27.873Z","repository":{"id":6715575,"uuid":"7961298","full_name":"rstudio/httpuv","owner":"rstudio","description":"HTTP and WebSocket server package for R","archived":false,"fork":false,"pushed_at":"2025-04-16T09:16:16.000Z","size":5675,"stargazers_count":241,"open_issues_count":55,"forks_count":88,"subscribers_count":23,"default_branch":"main","last_synced_at":"2025-05-13T23:26:57.334Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rstudio.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS.md","contributing":"CONTRIBUTING.md","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,"zenodo":null}},"created_at":"2013-02-01T16:17:25.000Z","updated_at":"2025-05-08T06:06:11.000Z","dependencies_parsed_at":"2023-02-19T01:01:19.522Z","dependency_job_id":"6f9b70bd-ac73-4f0a-9400-bff1a050a4fc","html_url":"https://github.com/rstudio/httpuv","commit_stats":{"total_commits":800,"total_committers":33,"mean_commits":"24.242424242424242","dds":"0.42000000000000004","last_synced_commit":"ff00e6b7c08d0bcf99f9d155bb16bed3a110a6cd"},"previous_names":[],"tags_count":46,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rstudio%2Fhttpuv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rstudio%2Fhttpuv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rstudio%2Fhttpuv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rstudio%2Fhttpuv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rstudio","download_url":"https://codeload.github.com/rstudio/httpuv/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254057526,"owners_count":22007544,"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-02T22:00:49.802Z","updated_at":"2025-05-14T08:09:22.860Z","avatar_url":"https://github.com/rstudio.png","language":"C","funding_links":[],"categories":["Web Technologies and Services","C"],"sub_categories":[],"readme":"# httpuv: HTTP and WebSocket server library for R\n\n  \u003c!-- badges: start --\u003e\n  [![R build status](https://github.com/rstudio/httpuv/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/rstudio/httpuv/actions)\n  \u003c!-- badges: end --\u003e\n\nhttpuv provides low-level socket and protocol support for handling HTTP and WebSocket requests directly from within R. It uses a multithreaded architecture, where I/O is handled on one thread, and the R callbacks are handled on another.\n\nIt is primarily intended as a building block for other packages, rather than making it particularly easy to create complete web applications using httpuv alone. httpuv is built on top of the [libuv](https://github.com/libuv/libuv) and [http-parser](https://github.com/nodejs/http-parser) C libraries, both of which were developed by Joyent, Inc.\n\n\n## Installing\n\nYou can install the stable version from CRAN, or the development version using **remotes**:\n\n```r\n# install from CRAN\ninstall.packages(\"httpuv\")\n\n# or if you want to test the development version here\nif (!require(\"remotes\")) install.packages(\"remotes\")\nremotes::install_github(\"rstudio/httpuv\")\n```\n\nSince httpuv contains C code, you'll need to make sure you're set up to install packages with compiled code. Follow the instructions at http://www.rstudio.com/ide/docs/packages/prerequisites\n\nhttpuv may optionally be built using a `libuv` system package, which you can install prior to installing the R package. It goes by different names on different package managers: `libuv1-dev` (deb), `libuv-devel` (rpm), `libuv` (brew). Version 1.43 or greater is required. If `libuv` is not found on the system, it will be built from source along with the R package.\n\n## Basic Usage\n\nThis is a basic web server that listens on port 8080 and responds to HTTP requests with a web page containing the current system time and the path of the request:\n\n```R\nlibrary(httpuv)\n\ns \u003c- startServer(host = \"0.0.0.0\", port = 8080,\n  app = list(\n    call = function(req) {\n      body \u003c- paste0(\"Time: \", Sys.time(), \"\u003cbr\u003ePath requested: \", req$PATH_INFO)\n      list(\n        status = 200L,\n        headers = list('Content-Type' = 'text/html'),\n        body = body\n      )\n    }\n  )\n)\n```\n\nNote that when `host` is 0.0.0.0, it listens on all network interfaces. If `host` is 127.0.0.1, it will only listen to connections from the local host.\n\nThe `startServer()` function takes an _app object_, which is a named list with functions that are invoked in response to certain events. In the example above, the list contains a function `call`. This function is invoked when a complete HTTP request is received by the server, and it is passed an environment object `req`, which contains information about HTTP request. `req$PATH_INFO` is the path requested (if the request was for http://127.0.0.1:8080/foo, it would be `\"/foo\"`).\n\nThe `call` function is expected to return a list containing `status`, `headers`, and `body`. That list will be transformed into a HTTP response and sent to the client.\n\nTo stop the server:\n\n```R\ns$stop()\n```\n\nOr, to stop all running httpuv servers:\n\n```R\nstopAllServers()\n```\n\n\n### Static paths\n\nA httpuv server application can serve up files on disk. This happens entirely within the I/O thread, so doing so will not block or be blocked by activity in the main R thread.\n\nTo serve a path, use `staticPaths` in the app. This will serve the `www/` subdirectory of the current directory (from when `startServer` is called) as the root of the web path:\n\n```R\ns \u003c- startServer(\"0.0.0.0\", 8080,\n  app = list(\n    staticPaths = list(\"/\" = \"www/\")\n  )\n)\n```\n\nBy default, if a file named `index.html` exists in the directory, it will be served when `/` is requested.\n\n`staticPaths` can be combined with `call`. In this example, the web paths `/assets` and `/lib` are served from disk, but requests for any other paths go through the `call` function.\n\n```R\ns \u003c- startServer(\"0.0.0.0\", 8080,\n  list(\n    call = function(req) {\n      list(\n        status = 200L,\n        headers = list(\n          'Content-Type' = 'text/html'\n        ),\n        body = \"Hello world!\"\n      )\n    },\n    staticPaths = list(\n      \"/assets\" = \"content/assets/\",\n      # Don't use index.html for /lib\n      \"/lib\" = staticPath(\"content/lib\", indexhtml = FALSE)\n    )\n  )\n)\n```\n\n\n### WebSocket server\n\nhttpuv also can handle WebSocket connections. For example, this app acts as a WebSocket echo server:\n\n```R\ns \u003c- startServer(\"127.0.0.1\", 8080,\n  list(\n    onWSOpen = function(ws) {\n      # The ws object is a WebSocket object\n      cat(\"Server connection opened.\\n\")\n\n      ws$onMessage(function(binary, message) {\n        cat(\"Server received message:\", message, \"\\n\")\n        ws$send(message)\n      })\n      ws$onClose(function() {\n        cat(\"Server connection closed.\\n\")\n      })\n    }\n  )\n)\n```\n\n\nTo test it out, you can connect to it using the [websocket](https://github.com/rstudio/websocket) package (which provides a WebSocket client). You can do this from the same R process or a different one.\n\n```R\nws \u003c- websocket::WebSocket$new(\"ws://127.0.0.1:8080/\")\nws$onMessage(function(event) {\n  cat(\"Client received message:\", event$data, \"\\n\")\n})\n\n# Wait for a moment before running next line\nws$send(\"hello world\")\n\n# Close client\nws$close()\n```\n\nNote that both the httpuv and websocket packages provide a class named `WebSocket`; however, in httpuv, that class acts as a server, and in websocket, it acts as a client. They also have different APIs. For more information about the WebSocket client package, see the [project page](https://github.com/rstudio/websocket).\n\n---\n\n\n## Debugging builds\n\nhttpuv can be built with debugging options enabled. This can be done by uncommenting these lines in src/Makevars, and then installing. The first one enables thread assertions, to ensure that code is running on the correct thread; if not. The second one enables tracing statements: httpuv will print lots of messages when various events occur.\n\n```\nPKG_CPPFLAGS += -DDEBUG_THREAD -UNDEBUG\nPKG_CPPFLAGS += -DDEBUG_TRACE\n```\n\nTo install it directly from GitHub with these options, you can use `with_makevars`, like this:\n\n```R\nwithr::with_makevars(\n  c(PKG_CPPFLAGS=\"-DDEBUG_TRACE -DDEBUG_THREAD -UNDEBUG\"), {\n    devtools::install_github(\"rstudio/httpuv\")\n  }, assignment = \"+=\"\n)\n```\n\n\u0026copy; 2013-2020 RStudio, Inc.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frstudio%2Fhttpuv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frstudio%2Fhttpuv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frstudio%2Fhttpuv/lists"}