6

websocketd

 6 years ago
source link: http://websocketd.com
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

websocketd

WebSockets
the UNIX way

Full duplex messaging between web browsers and servers

websocketd is the WebSocket daemon

It takes care of handling the WebSocket connections,
launching your programs to handle the WebSockets,
and passing messages between programs and web-browser.

It's like CGI, twenty years later, for WebSockets

Language agnostic

If you can run your program from the command line, you can write WebSocket endpoints.

No libraries required

Just read incoming text from stdin and write outgoing text to stdout.
Messaging is simple.

Avoid threading headaches

Each inbound WebSocket connection runs your program in a dedicated process.
Connections are isolated by process.

Built for the UNIX philosophy

Write programs that do one thing and do it well.
Write programs to work together.
Write programs to handle text streams,
because that is a universal interface.

If you dig that, you'll dig websocketd.

10 second tutorial

Let's create a tiny WebSocket server that counts to ten, pausing between each iteration.

Create a program in your favorite language:

  • Python
  • Swift
  • In fact... anything!
#!/bin/bash

# Count from 1 to 10 with a sleep
for ((COUNT = 1; COUNT <= 10; COUNT++)); do
  echo $COUNT
  sleep 0.5
done
  
     String  args  Exception 
      i i i 
      Systemouti
      Thread
    
  

 sys  stdout
 time  sleep

 count  range 
  count  
  stdoutflush
  sleep

sync  
  count
	puts count
	
#usrbinperl
use TimeHiRes sleep
# Autoflush
$
# Count from  to  with a sleep
 my $count    
	print 
	sleep 



  
     i

    
    stdout NULL

     i   i   i 
         i
        
    

     
 System
 SystemThreading

 

    
  
      i   i   i
    
      Consolei
      Thread
    
  
#usrbinenv xcrun swift
 

 index   
  index
  __stdoutp
  
import 

 
    i   i   i 
    i
     milliseconds 
  
You get the idea

If you can  a program that can be launched from
the  line you can build a WebSocket server

Read inbound messages from STDIN
Write outbound messages to STDOUT

Read HTTP and connection details from environment vars
Use line breaks as message delimiters

Start websocketd and tell it about your program:

$ websocketd --port=8080 my-program

Now connect to your program from JavaScript in a web-page using a WebSocket:

var ws = new WebSocket('ws://localhost:8080/');

ws.onmessage = function(event) {
  console.log('Count is: ' + event.data);
};

See... WebSocket servers are simple!

Whichever way inclined you are

I Groovy

  • Python
  • JavaScript
  • Clojure
  • Scala
  • Groovy
  • VB.Net
  • Swift
  • Objective-C
  • VBScript
  • Expect
  • AppleScript
  • Haskell
  • Pascal
  • Assembly
  • Fortran
  • Ocaml

Oh, and...

icon_16662.svg

Static server

Serves your static HTML, JavaScript, etc

icon_29158.svg

Program routing

Route different URLs to different programs

icon_65754.svg

CGI server

Dynamic generate content over HTTP too

icon_18460.svg

Out-of-the-box support for serving content using HTTPS and WSS

icon_4921.svg

Origin checking

Restrict which pages can make WebSocket connections

icon_59654.svg

Development console

Interact with your WebSocket programs before you've built your frontend

Announcements, new features, security advisories

We won't share your email address with anyone else. Pinky swear.

Created by Joe Walnes - Project lead: Alex Sergeyev

Written in Go - Forky forky on GitHub


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK