Server that relays messages to websocket clients (i.e. browsers). Useful for sending commands to applications via the command line, for example tell the browser to reload when a file changes.
npm i -g kabel
# Default port is 3900
kabel
# With different port as option
kabel -p 3010
# or with env variable
KABEL_PORT=3011 kabel
# You can also set the wait in ms before sending the message
kabel -w 500
# or with env variable
KABEL_WAIT=500 kabel
# Quiet output
kabel -q
# or with env variable
KABEL_QUIET=0
You only need one kabel server for all your apps.
Include this on in your web app:
<script>
var ws = new WebSocket('ws://localhost:3900')
ws.onopen = function() {
// Change the name to the name of your app
// The name should match what you're sending with curl
ws.send(JSON.stringify({ name: 'app' }))
}
ws.onmessage = function() {
window.location.reload(true)
}
ws.onerror = function() {
console.log('Error connecting to kabel')
}
</script>
Create a nodemon.json
file in your app root direcory that looks like this:
kabel.js notify 'http://localhost:3900?name=cfhqadmin'
{
"events": {
"restart": "kabel notify 'http://localhost:3900?name=app'"
}
}
Your application will now reload every time nodemon restarts.
Alternatively you can use curl
:
{
"events": {
"restart": "curl -X POST -d 'name=app' http://localhost:3900"
}
}
# From terminal using kabel
kabel notify 'http://localhost:3900?name=app'
# From terminal using curl
curl -X POST -d 'name=app' http://localhost:3900
Your app should now have reloaded.
MIT licensed. Enjoy!