Akita | A WebSocket CLI
A node.js command line interface for interacting with a WebSocket server.
Useful for sending specific or predefined messages over a WebSocket.
Table of contents
An example
akita ws://localhost:3000
> {"type": "message", "body": "Hello, world!"}
> @myCustomMessage
Installing
These are the different ways you can install, in no particular order.
npm i -g akita-ws
npm i --save-dev akita-ws
Usage
Below are the different things you can do.
akita ws://localhost:3000
akita echo
akita --help
akita echo --help
Predefined messages
You can use an .akitarc
file to specify predefined message you want to send.
akita uses cosmiconfig
to load config files, so you can use:
.akitarc
, akitarc.json
, akitarc.yml
or akitarc.js
if you want.
Say you have an .akitarc.yml
file:
messages:
helloWorld: A really long string payload
customData:
name: Geoff
age: 42
Then you can use do:
akita ws://localhost:3000
> @helloWorld
> @customData
Predefined URLs
You can also specify the WebSocket server's url in your .akitarc
file.
url: ws://localhost:3000
Then you can run akita without a url argument.
akita
You can send headers to the socket request with --header key:value
or with the headers
section of the yaml.
akita ws://localhost:3000 --header authorization:top_secret
or in .akitarc.yml
headers:
authorization: top_secret
Using the API
You can use akita programatically by importing it in TypeScript or JavaScript.
You might want to install it as a production dependancy in this case.
Here's an example script.js
:
const { Akita, EchoServer } = require('akita-ws')
const [, , cmd, ...args] = process.argv
if (cmd === 'run') {
const [url] = args
Akita.run({ url })
}
if (cmd === 'echo') {
const [port] = args
EchoServer.run(port)
}
I'm not sure what this is useful for, but it's possible.
For detailed usage see the source code.
Development
Below is information about development on the project.
Setup
To develop on this repo you will need to have node.js
installed on your dev machine and have an understanding of it.
This guide assumes you have the repo checked out and are on macOS.
You'll only need to follow this setup once for your dev machine.
npm install
touch .akitarc
Regular use
These are the commands you'll regularly run to develop the CLI, in no particular order.
npm run dev -s --
Irregular use
These are commands you might need to run but probably won't, also in no particular order.
npm run gen-readme-toc
npm run lint
npm run prettier
npm run build
npm run start
Code Structure
Folder | Contents |
---|
dist | Where the transpiled javascript and type definitions go |
node_modules | Where npm's modules get installed into |
src | Where the code for the CLI is |
Code formatting
This repo uses Prettier to automatically format code to a consistent standard.
It works using the husky
and lint-staged packages to
automatically format code whenever code is commited.
This means that code that is pushed to the repo is always formatted to a consistent standard.
You can manually run the formatter with npm run prettier
if you want.
Prettier is slightly configured in .prettierrc.yml
and also ignores files using .prettierignore.
Publishing
Use npm's version
and publish
command to push a new version to npmjs.com.
There is a preversion
that does a few things:
- Runs unit tests
- Generates the table of contents in this readme
- Transpiles typescript assets into javascript & type definitions
- Adds those changes to git
Future work
Some ideas I've had for where I want this project to go.
- Parameterised / templated predefined messages
- Different / custom serialisation methods (other that
JSON.stringify
) - Add unit test coverage
- Single usage like
akita ws://localhost @customData @hello ...
- Read the
akitarc
on the fly incase it changes after starting - Document piped usage,
cat data.json | akita
This project was setup with robb-j/ts-node-base