livecode icon indicating copy to clipboard operation
livecode copied to clipboard

Feature Request: stream to the outside world

Open bitsandbricks opened this issue 4 years ago • 9 comments

I've found that is relatively simple to enable public access to the file served by livecode using a free service like ngrok.

I made a brief tutorial, here: https://bitsandbricks.github.io/post/code-live-from-rstudio-and-share-it-with-the-world-in-real-time/

Could ngrok integration be rolled into livecode? i.e in a similar fashion to the already present bit.ly support.

bitsandbricks avatar Mar 19 '20 20:03 bitsandbricks

I like the blog post a lot, thanks for putting it together.

I had not heard of ngrok before, the service looks quite useful and definitely something we could add support for. Seems like ngrok should have its own interface package but it looks like there is a nice local rest API for querying the tunnel.

rundel avatar Mar 19 '20 22:03 rundel

Brilliant! A baby step toward integration, the following helper command opens a new command window and runs the ngrok utility with the local server URL, so all you have to do is copy the ngrok.io URL and share it with your students.

s <- livecode::serve_file(bitly=FALSE) 
system(paste0("ngrok http ", s$url), wait = FALSE, invisible = FALSE,
       show.output.on.console = FALSE, minimized = FALSE)

ajlyons avatar Apr 04 '20 07:04 ajlyons

Brilliant! A baby step toward integration, the following helper command opens a new command window and runs the ngrok utility with the local server URL, so all you have to do is copy the ngrok.io URL and share it with your students.

s <- livecode::serve_file(bitly=FALSE) 
system(paste0("ngrok http ", s$url), wait = FALSE, invisible = FALSE,
       show.output.on.console = FALSE, minimized = FALSE)

Works for me! Thank you

bitsandbricks avatar Apr 07 '20 17:04 bitsandbricks

I'm wondering if anyone else is having trouble with ngrok (or similar) recently. I used ngrok last fall to stream out my live code files and it worked great. However, now I'm trying to use it and all I see is a blank page. Everything else appears to be working (the ngrok interface shows me hitting the page), but the page remains blank. Any thoughts?

BTW I'm on an M1 Mac with Monterey 12.3.1, R v 4.1.3, livecode v 0.1.0.9000. I also get the same result using an Intel Mac with BugSur 11.5.2, R 4.1.0 and the same livecode version. One more update - tried on the newest R 4.2.0 on my intel mac, same story.

When I serve an R file that contains the following:

library(dplyr)
library(ggplot2)
dat <- mtcars %>% select(c(1, 3:7))
combs <- combn(names(dat), 2) %>% 
  t() %>% 
  as.data.frame()
plots <- purrr::map2(combs$V1, combs$V2, ~ggplot(dat, aes_string(x=.x, y=.y)) + geom_point())
plots

This is what ngrok serves on the other end.

<html><head>
    <title>/Users/david/x.r</title>

    <link href="web/progressbar/progressbar.css" rel="stylesheet">
    <script src="web/progressbar/progressbar.min.js"></script>

    <link href="web/prism/themes/prism-custom.css" rel="stylesheet">
    <script src="web/prism/langs/prism-core.min.js"></script>
    <script src="web/prism/langs/prism-r.min.js"></script>

    <script src="web/prism/plugins/prism-normalize-whitespace.min.js"></script>

    <script src="web/prism/plugins/prism-line-numbers.min.js"></script>
    <link href="web/prism/plugins/prism-line-numbers.css" rel="stylesheet">

    <script src="web/prism/plugins/prism-line-highlight.min.js"></script>
    <link href="web/prism/plugins/prism-line-highlight.css" rel="stylesheet">

    <link href="web/noty/noty.css" rel="stylesheet">
    <script src="web/noty/noty.min.js" type="text/javascript"></script>

    <link href="web/noty/themes/bootstrap-v3.css" rel="stylesheet">
    <link href="web/noty/themes/bootstrap-v4.css" rel="stylesheet">
    <link href="web/noty/themes/light.css" rel="stylesheet">
    <link href="web/noty/themes/metroui.css" rel="stylesheet">
    <link href="web/noty/themes/mint.css" rel="stylesheet">
    <link href="web/noty/themes/nest.css" rel="stylesheet">
    <link href="web/noty/themes/relax.css" rel="stylesheet">
    <link href="web/noty/themes/semanticui.css" rel="stylesheet">
    <link href="web/noty/themes/sunset.css" rel="stylesheet">

    <script src="web/livecode/livecode.js"></script>
    <link href="web/livecode/livecode.css" rel="stylesheet">

    <base target="_blank">
  </head>
  <body>
    <pre class="line-numbers livecode language-r"><code class=" language-r">
      </code></pre>
  <div id="progressbar"></div>
  
</body></html>

davidaarmstrong avatar May 04 '22 19:05 davidaarmstrong

@davidaarmstrong, since version 2.3 of ngrok traffic is automatically forwarded through HTTPS instead of HTTP.

You can revert to http by running:

ngrok http http://xxx.xx.xx.xxx:yyyy --scheme http --scheme https

nareal avatar Oct 14 '22 15:10 nareal

@nareal Apologies for not thanking you for this earlier. It just became relevant to me again, so I looked back to see what the status was. Your suggestion worked perfectly - it really saved my lecture today.

davidaarmstrong avatar Feb 27 '23 14:02 davidaarmstrong

@davidaarmstrong I'm glad it helped.

nareal avatar Feb 27 '23 16:02 nareal

@davidaarmstrong, since version 2.3 of ngrok traffic is automatically forwarded through HTTPS instead of HTTP.

You can revert to http by running:

ngrok http http://xxx.xx.xx.xxx:yyyy --scheme http --scheme https

This solution is not working for me. I'm on ngrok 3.2.2 and when I open the http link it gets redirected to the https version and I get the blank page again.

I was able to work around the issue by using http://localhost.run/.

livecode::serve_file("file", ip = "127.0.0.1")

and then

ssh -R 80:localhost:xxxxx localhost.run

on the terminal, replacing the xxx with the proper port.

eliocamp avatar May 09 '23 21:05 eliocamp

I believe the problem is that the browser is trying to connect to the websocket using an insecure connection (ws). This can be solved by adding a directive to upgrade insecure requests. I have made a pull request (#16) that adds a parameter that allows this when creating the server:

s <- livecode::serve_file(ip = "127.0.0.1", bitly=FALSE, upgrade_content_security_policy = TRUE)

and then ngrok will work as expected when called by:

ngrok http http://xxx.xx.xx.xxx:yyyy --scheme http --scheme https

nareal avatar May 17 '23 08:05 nareal