nanonext - High Performance Persistent HTTP Sessions

R

ncurl sessions

shikokuchuo
2023-01-26


Persistent http(s) sessions is a new feature added in nanonext 0.7.3.

This allows for efficient polling by keeping an open connection with the server, transacting as and when needed.

Provides an ideal, low-latency solution to requesting real time data over a REST API, especially when there are limits in place for the frequency of new connections.

ncurl_session()

Create a session (persistent connection):

library(nanonext)
sess <- ncurl_session("https://httpbin.org/headers")

transact()

Transact over the session (repeatedly if required):

res <- transact(sess)
res
$status
[1] 200

$headers
NULL

$raw
  [1] 7b 0a 20 20 22 68 65 61 64 65 72 73 22 3a 20 7b 0a 20 20 20 20
 [22] 22 48 6f 73 74 22 3a 20 22 68 74 74 70 62 69 6e 2e 6f 72 67 22
 [43] 2c 20 0a 20 20 20 20 22 58 2d 41 6d 7a 6e 2d 54 72 61 63 65 2d
 [64] 49 64 22 3a 20 22 52 6f 6f 74 3d 31 2d 36 33 64 32 64 64 38 63
 [85] 2d 32 61 66 61 34 64 65 33 32 37 34 35 36 62 30 34 30 33 35 34
[106] 34 63 33 39 22 0a 20 20 7d 0a 7d 0a

$data
[1] "{\n  \"headers\": {\n    \"Host\": \"httpbin.org\", \n    \"X-Amzn-Trace-Id\": \"Root=1-63d2dd8c-2afa4de327456b0403544c39\"\n  }\n}\n"

Timing

Allows much lower latencies in returning results:

library(microbenchmark)

microbenchmark(transact(sess), ncurl("https://httpbin.org/headers"))
Unit: milliseconds
                                 expr       min        lq     mean
                       transact(sess)  78.14837  82.39269 160.0736
 ncurl("https://httpbin.org/headers") 474.78558 488.71817 793.1671
    median       uq        max neval
  84.85723 140.6003   784.5898   100
 498.19017 516.3694 15475.3540   100

{nanonext} package website: https://shikokuchuo.net/nanonext/
On CRAN: https://cran.r-project.org/package=nanonext

{nanonext} features in the ‘Web Technologies’ CRAN Task View under ‘Core Tools For HTTP Requests’: https://cran.r-project.org/view=WebTechnologies and also in the ‘High Performance Computing’ CRAN Task View: https://cran.r-project.org/view=HighPerformanceComputing

Citation

For attribution, please cite this work as

shikokuchuo (2023, Jan. 26). shikokuchuo{net}: nanonext - High Performance Persistent HTTP Sessions. Retrieved from https://shikokuchuo.net/posts/20-ncurl-sessions/

BibTeX citation

@misc{shikokuchuo2023nanonext,
  author = {shikokuchuo, },
  title = {shikokuchuo{net}: nanonext - High Performance Persistent HTTP Sessions},
  url = {https://shikokuchuo.net/posts/20-ncurl-sessions/},
  year = {2023}
}