Skip to content

Ten second tutorial

Joe Walnes edited this page Jul 11, 2014 · 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

No time now! Do it later.

2. Create a program that outputs data to STDOUT

We'll use bash. But you can use any language you like. websocketd has been used to create servers in Python, R, C, C#, AppleScript, PHP, pretty much anything (though I don't recommend JVM based languages).

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:

var ws = new WebSocket('ws://localhost:8080/');
    
ws.onmessage = function(event) {
  console.log('Count is: ' + event.data);
};

Done

Ok, got it?

Wanna learn more? Read the slightly more verbose ten MINUTE tutorial instead.

Clone this wiki locally