2019-02-13

Turn any program that uses STDIN/STDOUT into a WebSocket server using websocketd

Yesterday I tried for the first time websocketd and it is amazing!
This is a little gem created by @joewalnes that let you implement language agnostic WebSocket applications based on any command line program reading text from stdin and writing a message output to stdout. A simple (and genial) approach.

If think that it is perfect for testing too, so I updated an existing integration test for a WebSocket client  of mine using a local websocketd server on TravisCI instead of relying on external public services requiring internet as done before (wss://echo.websocket.org). This activity was already on my roadmap so why not try out websocketd?!

As you can see here (https://github.com/davidemoro/pytest-play-docker/pull/42/files): in .travis.yml I added a before_script calling a travis/setup_websocket.sh script that installs and runs websocketd in background on port 8081 based on a simple travis/echo_ws.sh that reads a line from stdin echoing to stdout.

The websocketd syntax is the following:

./websocketd --port=8081 ./echo_ws.sh

were echo_ws.sh can be any executable stdin/stdout based. More details in next section.

Wanna tryout websocketd?

Download a websocketd version compatible with your OS/architecture and unzip the folder from http://websocketd.com/#download: it contains a websocketd executable ready to be used so no installation needed and follow the tutorials described in https://github.com/joewalnes/websocketd/wiki.

Alternatively you can test websocketd using pytest-play's play_websocket plugin that is ready to be used assuming that you have Docker installed.
docker run --rm -it -v $(pwd):/src --network host davidemoro/pytest-play --variables variables.yml
Additional links you might find useful for this example:

No comments:

Post a Comment

Note: only a member of this blog may post a comment.