Aim:
I am trying to use my Linux terminal from my web application.
Setup
NodeJS
ReactJS
Ubuntu Server 20.04
Description
I want to create an application so that I can write commands in my browser, and NodeJS executed them on the backend and return the output.
Now, I know this is very simple using the spawn process, and I can await response and then send it back to the frontend, but I want it to be dynamic. The data is sent in real-time. So if the apt install command is being executed, I could see the stdout in real time on my browser window. How can I achieve it? Even if I use a web socket, how do I receive it from the terminal in real-time, because afaik, spawn process returns the output all at once.
Related
I familiar to Node.js and just a little bit about web programming. One of my CLI Node app need to let user see the running program logs. I hope she can open a separated browser and point to something like http://localhost:12345, then she got live log keeping scrolling in the page without any human interaction.
Is that a simple method to do such kind of application? I know programming RESTful, not sure if it helps.
If I understood your question correctly, you are trying to show live server side logs to the user. For that you will have to tail the log file and pipe the output to response or pipe the stdout (if you're not writing logs to file) to the response using socket.io connection. socket.io is a way of providing live updates to the users without them sending https request every time. You can see example here .
TL;DR: how to attach a docker container bash to a node.js stream?
I need to implement a relay between a docker container bash and the final user. The app is a remote compile/run for c/cpp, Python and JS. Some references (repl.it, cpp.sh). To accomplish that my plan it's to:
Instantiate a Ubuntu docker container with the requirements for
compiling and running the user code.
Prompt some bash commands for compiling/running the user code.
And finally, output the resulting from the bash console to the user.
I've found some repo's with interesting code they are: compilebox, dockerrode and docker-api.
The 1st, do the task using containers and some promise/async black magic to compile, pipe out to a file and send to the user through HTTP (get/post). My problem with this one is because I need to establish a shell-like environment for my user. My goal is to bring a bash window over the browser.
The 2nd and 3rd implements API based on the official HTTP docker engine API (I took the v1.24 because this one has an overview for layman's like me). Both have examples of some sort of IO Stream between two entities. Like this, the duplexstream, but because of some mistake of implementation, the IO doesn't work properly (Issue#455).
So my problem is: how to attach a docker container bash to a node.js stream? So when it's done, everything that the user types in the app on the browser it's sent via HTTP to the bash container and the output is sent back as well.
I have a node script I'm working on that connects to an IRC channel for twitch.tv and responds to commands, as well as moderates chat lines that are being sent by other users connected.
Currently I open a terminal to the file location and run node app.js to start the IRC connection. But this runs on my mac and I use my PC to play games and watch the chat, so I have to have the Mac next to me to start and stop the IRC chat client app.
I want to move this to a web server where I can log in and start or stop the chat client app from the website, so I don't have to have the Mac next to me all the time.
What would be the best way to go about this?
If you want to see the script I'm working with you can find it here
https://github.com/Jordan4jc/super-fly-twitch-bot
You could create an HTTP server and listen on a port (if you don't mind overkill, express.js could make things easier for you). This server would contain configurations for the URL, authentication, etc. Within the callback that you provide to a URL path, you could close the IRC channels, database connections, and call process.exit(0) once you're confident that you've done everything necessary and you're sure it's what you want.
You can use socket.io as a way to send signals in real time to the application.
Let's say I have a program running in my local machine, and I would like it to listen to HTTP requests, which triggers the program to run and respond back with the output of the program. For example, I have rrdtool (command line utility for accessing round robin database) installed in my Linux box, and I want web clients to request the web server to run the rrdtool program and respond back with the output of rrdtool.
Quesitons:
I know programming languages are used to generate dynamic html contents to be sent back to clients, but rrdtool is an already existing program that just needs to be triggered by a web request. In fact, rrdtool provides various programming language bindings such as python, but I would like to use Node.js on the server side and javascript binding to rrdtool isn't supported. So how is the interface between my javascript code and the rrdtool program (CLI) done?
I thought about using a Java implementation of rrdtool functionalities such as rrd4j, but portability isn't really a priority for me and I would like to run the official rrdtool program (written in C) for a better performance. However, I'm not sure if the cost of the interface between javascript on the server side and rrdtool would outweigh the performance benefits of running the C program.
Any help/feedback/pointers would be appreciated.
Just use NodeJS's "child_process" module to run your rrdtool. Then capture its output with child.stdin.on('data', function (data) {..., etc. Grab its output, format it into HTML, and send it as a reply to the web request that initiated it.
If you are using RRDTool to generate graph images, then you should be able to call rrdtool via your NodeJS handler, having it write to a temporary file in a web accessible directory. Then, send back the URL of the newly created image file, which your web frontend can use to create a new IMG tag for display.
I'm developing a simple 2d online game and now I'm designing my server. The server will be run on linux vps and I need a way to communicate with it (for example to close it, and as it will be run on vps, simply closing terminal won't work). So I think there are 2 options:
1) Write 2 apllications - server which doesn't say anything and doesn't accept console input and the second application is console which sends commands to server (like exit, get online players etc).
2) Write 1 application which has 2 threads - one is the real server, the second thread will be used for cin and cout. However I'm not sure if this will work on vps...
Or maybe there is better aproach? What is the usual way of doing this?
Remember that it must be vps-compatible way (only ssh access to it).
Thanks
I would go for a "daemon" (server) for the main server function and then use a secondary application that can connect to the server and send it commands.
Or just use regular signals, like most other servers do - when you reconfigure your Apache server, for example, you send it a SIGHUP signal that restarts the server. That way, you don't need a second application at all - just "kill -SIGHUP your_server_pid".