• Stars
    star
    17,086
  • Rank 1,512 (Top 0.04 %)
  • Language
    Go
  • License
    BSD 2-Clause "Sim...
  • Created about 11 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Turn any program that uses STDIN/STDOUT into a WebSocket server. Like inetd, but for WebSockets.

websocketd

websocketd is a small command-line tool that will wrap an existing command-line interface program, and allow it to be accessed via a WebSocket.

WebSocket-capable applications can now be built very easily. As long as you can write an executable program that reads STDIN and writes to STDOUT, you can build a WebSocket server. Do it in Python, Ruby, Perl, Bash, .NET, C, Go, PHP, Java, Clojure, Scala, Groovy, Expect, Awk, VBScript, Haskell, Lua, R, whatever! No networking libraries necessary.

-@joewalnes

Details

Upon startup, websocketd will start a WebSocket server on a specified port, and listen for connections.

Upon a connection, it will fork the appropriate process, and disconnect the process when the WebSocket connection closes (and vice-versa).

Any message sent from the WebSocket client will be piped to the process's STDIN stream, followed by a \n newline.

Any text printed by the process to STDOUT shall be sent as a WebSocket message whenever a \n newline is encountered.

Download

If you're on a Mac, you can install websocketd using Homebrew. Just run brew install websocketd. For other operating systems, or if you don't want to use Homebrew, check out the link below.

Download for Linux, OS X and Windows

Quickstart

To get started, we'll create a WebSocket endpoint that will accept connections, then send back messages, counting to 10 with 1 second pause between each one, before disconnecting.

To show how simple it is, let's do it in Bash!

count.sh:

#!/bin/bash
for ((COUNT = 1; COUNT <= 10; COUNT++)); do
  echo $COUNT
  sleep 1
done

Before turning it into a WebSocket server, let's test it from the command line. The beauty of websocketd is that servers work equally well in the command line, or in shell scripts, as they do in the server - with no modifications required.

$ chmod +x count.sh
$ ./count.sh
1
2
3
4
5
6
7
8
9
10

Now let's turn it into a WebSocket server:

$ websocketd --port=8080 ./count.sh

Finally, let's create a web-page to test it.

count.html:

<!DOCTYPE html>
<pre id="log"></pre>
<script>
  // helper function: log message to screen
  function log(msg) {
    document.getElementById('log').textContent += msg + '\n';
  }

  // setup websocket with callbacks
  var ws = new WebSocket('ws://localhost:8080/');
  ws.onopen = function() {
    log('CONNECT');
  };
  ws.onclose = function() {
    log('DISCONNECT');
  };
  ws.onmessage = function(event) {
    log('MESSAGE: ' + event.data);
  };
</script>

Open this page in your web-browser. It will even work if you open it directly from disk using a file:// URL.

More Features

  • Very simple install. Just download the single executable for Linux, Mac or Windows and run it. Minimal dependencies, no installers, no package managers, no external libraries. Suitable for development and production servers.
  • Server side scripts can access details about the WebSocket HTTP request (e.g. remote host, query parameters, cookies, path, etc) via standard CGI environment variables.
  • As well as serving websocket daemons it also includes a static file server and classic CGI server for convenience.
  • Command line help available via websocketd --help.
  • Includes WebSocket developer console to make it easy to test your scripts before you've built a JavaScript frontend.
  • Examples in many programming languages are available to help you getting started.

User Manual

More documentation in the user manual

Example Projects

Got more examples? Open a pull request.

My Other Projects

And follow @joewalnes!

More Repositories

1

reconnecting-websocket

A small decorator for the JavaScript WebSocket API that automatically reconnects
JavaScript
4,168
star
2

smoothie

Smoothie Charts: smooooooth JavaScript charts for realtime streaming data
JavaScript
2,230
star
3

filtrex

A simple, safe, JavaScript Filter Expression compiler for end-users
JavaScript
1,042
star
4

web-vmstats

Prettify vmstats in your browser
JavaScript
1,009
star
5

gcode-viewer

A web-based 3D GCode viewer to help understand 3D printer models.
JavaScript
246
star
6

jstinytest

Tiniest simplest unit testing library for JavaScript
JavaScript
204
star
7

greendots

Write with those green dots on your GitHub profile
Python
184
star
8

tinytest

A tiny unit-testing framework for C
C
170
star
9

jquery-simple-context-menu

A really simple, good looking jQuery context menu
JavaScript
102
star
10

port-sharding

Example of Linux SO_REUSEPORT
Python
90
star
11

go-getter

Like 'go get' but with pinned package versions. A tiny shell script.
Shell
86
star
12

toybrick

OpenSCAD program to generate toy bricks compatible with existing systems
OpenSCAD
85
star
13

node-ldapauth

LDAP authentication library for Node.JS
C++
69
star
14

resisto.rs

No frills resistor color code tool
HTML
67
star
15

webconnect

JavaScript APIs to allow web-apps to connect to hardware devices.
JavaScript
52
star
16

pi-crust

Connect things to your Raspberry Pi
Eagle
47
star
17

idea-community

IntelliJ IDEA Community edition - my hacks
Java
33
star
18

crunchicorn

Simplifying the web toolchain
JavaScript
32
star
19

verifysignature

Sample of standalone portable C to verify Ed25519 public-key signature
C
21
star
20

hackerspace-globe

Mashup: View HackerSpace status on a WebGL Globe
JavaScript
14
star
21

raspberry-play

Playing with the Raspberry Pi
JavaScript
14
star
22

quick2wire-fritzomatic

A tool to quickly generate Fritzing components.
JavaScript
9
star
23

flashprance

A lightweight communication protocol to send data from your computer/phone to an embedded device by flashing the screen
C++
9
star
24

arduino-play

Playing with Arduino and AVR hardware
C
7
star
25

webbit-easyremote

Simple bidirectional JavaServer<->JavaScriptBrowser remoting, based on WebSockets and Webbit
JavaScript
6
star
26

webbit

Webbit Server: Moved....
5
star
27

webbit-asyncio

Event based asynchronous Java IO library - built on libeio
C
5
star
28

msp430-shiftbrite

C / C++ library to allow MSP430 microcontroller to talk to ShiftBrite RGB module
C++
5
star
29

esper-console

Web based Esper console
JavaScript
3
star
30

thor-gui

A web based GUI for Thor
JavaScript
2
star
31

3d-printing

Thingies I've designed for 3D printing
2
star
32

webbit-sitemesh

SiteMesh 3 for Webbit
Java
2
star
33

graphviz2

Joe's modifications to AT&T's GraphViz/Dot
C
2
star
34

c-stuff

C
1
star
35

vim

.vim
Vim Script
1
star
36

pi-plate-clip

Makes the Adafruit's Raspberry Pi Plate just a liiiiittle bit stronger.
1
star
37

openscad-tutorial

A tutorial for OpenSCAD.
1
star
38

test

1
star
39

sitemesh-resources

Additional resources to support SiteMesh project.
1
star
40

domain-watcher

Watches a domain name and notifies me when it's status changes
1
star
41

msp430-rgb-color-cycle

A quick cheap circuit for color cycling an RGB LED using an MSP430 microcontroller.
1
star
42

atx-psu-breakout

Fork of the ATX PSU breakout board from Dangerous Prototypes.
1
star