Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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 65 61 35 [85] 2d 30 64 63 64 38 61 66 37 36 34 64 34 65 65 38 34 35 37 63 39 [106] 34 64 32 36 22 0a 20 20 7d 0a 7d 0a $data [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-63d2dea5-0dcd8af764d4ee8457c94d26\"\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) 81.16248 83.44756 153.3151 ncurl("https://httpbin.org/headers") 476.65439 489.24140 569.3550 median uq max neval 87.31694 127.2824 855.6107 100 497.40339 602.0486 1557.0635 100
Links
{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
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.