Skip to content

Ten second tutorial

joewalnes edited this page Mar 22, 2013 · 12 revisions

This is a really quick run through of getting a WebSocket application up and running with websocketd. Quick, only eight seconds left!

1. Download and install websocketd

Instructions here

2. Create a program that outputs data to STDOUT

We'll use bash. But you can use anything.

count.sh:

#!/bin/bash

# Count from 1 to 10, pausing for a second between each iteration.
for COUNT in $(seq 1 10); do
  echo $COUNT
  sleep 1
done

Make it executable:

$ chmod +x ./count.sh

3. Start the websocketd server

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

4. Use JavaScript to connect to your WebSocket

In a web-page:

<script> var ws = new WebSocket('ws://localhost:8080/'); ws.onmessage = function(event) { console.log('Count is: ' + event.data); }; </script>
Clone this wiki locally