• Stars
    star
    375
  • Rank 109,839 (Top 3 %)
  • Language
    Lua
  • License
    MIT License
  • Created about 12 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

Websockets for Lua.

Not maintained / maintainer wanted !!!!

If someone wants to maintain / take ownership of this project, reach out to me (issue, email). I like Lua very much, but I don't have enough time / resources to stay engaged with it.

About

This project provides Lua modules for Websocket Version 13 conformant clients and servers. Build Status Coverage Status

The minified version is only ~10k bytes in size.

Clients are available in three different flavours:

  • synchronous
  • coroutine based (copas)
  • asynchronous (lua-ev)

Servers are available as two different flavours:

A webserver is NOT part of lua-websockets. If you are looking for a feature rich webserver framework, have a look at orbit or others. It is no problem to work with a "normal" webserver and lua-websockets side by side (two processes, different ports), since websockets are not subject of the 'Same origin policy'.

Usage

copas echo server

This implements a basic echo server via Websockets protocol. Once you are connected with the server, all messages you send will be returned ('echoed') by the server immediately.

local copas = require'copas'

-- create a copas webserver and start listening
local server = require'websocket'.server.copas.listen
{
  -- listen on port 8080
  port = 8080,
  -- the protocols field holds
  --   key: protocol name
  --   value: callback on new connection
  protocols = {
    -- this callback is called, whenever a new client connects.
    -- ws is a new websocket instance
    echo = function(ws)
      while true do
        local message = ws:receive()
        if message then
           ws:send(message)
        else
           ws:close()
           return
        end
      end
    end
  }
}

-- use the copas loop
copas.loop()

lua-ev echo server

This implements a basic echo server via Websockets protocol. Once you are connected with the server, all messages you send will be returned ('echoed') by the server immediately.

local ev = require'ev'

-- create a copas webserver and start listening
local server = require'websocket'.server.ev.listen
{
  -- listen on port 8080
  port = 8080,
  -- the protocols field holds
  --   key: protocol name
  --   value: callback on new connection
  protocols = {
    -- this callback is called, whenever a new client connects.
    -- ws is a new websocket instance
    echo = function(ws)
      ws:on_message(function(ws,message)
          ws:send(message)
        end)

      -- this is optional
      ws:on_close(function()
          ws:close()
        end)
    end
  }
}

-- use the lua-ev loop
ev.Loop.default:loop()

Running test-server examples

The folder test-server contains two re-implementations of the libwebsocket test-server.c example.

cd test-server
lua test-server-ev.lua
cd test-server
lua test-server-copas.lua

Connect to the from Javascript (e.g. chrome's debugging console) like this:

var echoWs = new WebSocket('ws://127.0.0.1:8002','echo');

Dependencies

The client and server modules depend on:

  • luasocket
  • luabitop (if not using Lua 5.2 nor luajit)
  • luasec
  • copas (optionally)
  • lua-ev (optionally)

Install

$ git clone git://github.com/lipp/lua-websockets.git
$ cd lua-websockets
$ luarocks make rockspecs/lua-websockets-scm-1.rockspec

Minify

A squishy file for squish is provided. Creating the minified version (~10k) can be created with:

$ squish --gzip

The minifed version has be to be installed manually though.

Tests

Running tests requires:

docker build .

The first run will take A WHILE.

More Repositories

1

login-with

Stateless login-with microservice for OAuth
JavaScript
2,318
star
2

doclets

Hosted automated jsdoc API documentation
JavaScript
205
star
3

next-todos

200 lines realtime todos app powered by next.js, preact, jet, redux and now
JavaScript
125
star
4

tango

A simple and transparent RPC module for Lua.
Lua
54
star
5

hackernews

100 lines of code Hackernews reader (with the help of next.js).
JavaScript
45
star
6

nodish

A Lightweight Lua equivalent to Node.js
Lua
38
star
7

lua-jet

Realtime Message Bus for the Web. Lua Implementation
Lua
32
star
8

img-min

A responsive, lazy, microservice/CDN backed image web-component
JavaScript
32
star
9

ludent

A primitive Lua indenter / formatter / beautfier
Lua
19
star
10

likes

Easily query social media likes/followers without tokens (for node)
JavaScript
16
star
11

zbus

A simple TCP/IP based message bus in Lua.
Lua
12
star
12

Arduino-Jet

Jet lib for Arduino (http://jetbus.io)
C++
9
star
13

jet-chat

A jet and next.js powered chat demo.
JavaScript
9
star
14

tinker

Lua module for tinkerforge bricks and bricklets
Lua
5
star
15

lualibusb

the unmodified lualibusb 1.0 brought to github with rockspec
Lua
5
star
16

img-scale

An image scaling microservice
JavaScript
4
star
17

lua-step

Un-nest asynchronous control flow.
Lua
4
star
18

wappcam

A web application for remote camera control. The student's output of my lecture at the Hochschule Darmstadt.
JavaScript
4
star
19

fit-text

A fit text to width responsive web component
JavaScript
4
star
20

jet-js

Jet for Browser. Deprecated repo.
JavaScript
3
star
21

lazycats

A lazy loading list of cat gifs using `IntersectionObserver`
JavaScript
3
star
22

enterjs-nextjs-news

Source from my live coding / talk at the EnterJS 2019
JavaScript
2
star
23

react-router-hash-route

Enable hash / id based routes with react router
JavaScript
2
star
24

react-array-perf

React array performance measurement
JavaScript
2
star
25

emitter

node.js inspired integration of lua-ev with sockets,etc
Lua
2
star
26

acme-jsdoc-example

Test repo for doclets.io
JavaScript
1
star
27

next-webcomponents-test

JavaScript
1
star
28

config

xressources emacs etc
1
star
29

slocit

JavaScript
1
star
30

dotfiles

Lua
1
star
31

test-sb

1
star
32

next-eval-bug

JavaScript
1
star
33

next-playtime

JavaScript
1
star
34

gatsby-starter-netlify-cms

JavaScript
1
star
35

lua-ev-nexttick

The lua-ev equivalent to node.js process.nextTick
Lua
1
star
36

libwebsockets-exp

NOT REQUIRED ANY MORE. modify libwebsocket to make binding it to Lua easier
C
1
star
37

next-11-1-0-preact-bug

Bug demo
JavaScript
1
star
38

Todo-Next.js-noscript

Todo App written in Next.js which works with JavaScript disabled
JavaScript
1
star
39

next-now-github-ci-cd

Testing Next.js with the Now GitHub CI/CD
Dockerfile
1
star