1 unstable release
0.1.0 | Jul 24, 2022 |
---|
#266 in WebSocket
135KB
327 lines
cmdpiped
cmdpiped
is a command-line tool for exposing a wrapped cli program's standard IO to WebSockets/SSE
Installation
Ready to use Binaries are available at the releases page.
For Rust users, you can always use cargo
cargo install cmdpiped
Usage
USAGE:
cmdpiped [OPTIONS] --mode <MODE> [SUBCOMMAND]
OPTIONS:
-h, --host <HOST> Host address to bind [default: 127.0.0.1]
--help Print help information
-m, --mode <MODE> Mode to expose events [possible values: ws, sse]
-p, --port <PORT> Port to bind [default: 9000]
--path <PATH> Url path to setup [default: /events]
--serve <SERVE> Optional folder path to serve static files
-V, --version Print version information
You can also use piping
$ node ./index.js | cmdpiped -m sse
Examples
Some Basic Examples
cmdpiped
is language agnostic and should be able to plugin easily for anything that can run on the command line.
Python
from sys import stdout
from time import sleep
# Count to 100 sleeping every second
for count in range(0, 100):
print(count + 1)
stdout.flush()
sleep(1)
Save the file as counter.py
and run the cmdpiped
$ cmdpiped -m sse python3 ./counter.py
Node.js
let count = 0;
setInterval(() => {
console.log(count++);
}, 1000);
Save the file as index.js
and run the cmdpiped
$ cmdpiped -m sse node ./index.js
Bash
#!/bin/bash
# Count from 1 to 100 with a sleep of 1 second
for ((COUNT = 1; COUNT <= 100; COUNT++)); do
echo $COUNT
sleep 1
done
Save the file as script.sh
and run the cmdpiped
$ chmod +x ./script.sh
$ cmdpiped -m sse ./script.sh
You should be able to get:
[2022-07-24T13:41:11Z TRACE actix_server::worker] Service "actix-web-service-127.0.0.1:9000" is available
[2022-07-24T13:41:11Z TRACE cmdpiped::broadcaster] Send: "data: 1\n\n"
[2022-07-24T13:41:11Z TRACE cmdpiped::broadcaster] Send: "data: 2\n\n"
[2022-07-24T13:41:12Z TRACE cmdpiped::broadcaster] Send: "data: 3\n\n"
[2022-07-24T13:41:13Z TRACE cmdpiped::broadcaster] Send: "data: 4\n\n"
...
Advanced Example
Using the above example, we can add some frontend code
- Create a folder called
static
. - Add an
index.html
file:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var source = new EventSource("http://localhost:9000/events");
source.onmessage = function (event) {
var content = document.getElementById("content");
content.innerHTML = content.innerHTML + event.data + "<br/>";
};
</script>
</head>
<body>
<div id="content"></div>
</body>
</html>
Run cmdpiped
exposing a directory
$ cmdpiped -m sse --serve ./static
A complete example
A complete example is available in the examples
Roadmap
- SSE Streaming
- Websocket Server -> Client
- Websocket Client -> Server
Versioning
We use SemVer for versioning. For the versions available, see the tags on this repository.
Authors
- Njuguna Mureithi - Initial work - Njuguna Mureithi
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Dependencies
~19–32MB
~550K SLoC