local ws = WebSocket()
local ws = WebSocket{
    url = string,
    onreceive = function(message, data),
    deflate = bool,

Creates a websocket client that can be used to communicate with another program:

  • url specifies the server to connect.
  • deflate option enables compression before sending the data - if the server is running on the same machine, it might be faster to turn it off.
  • onreceive function will be called for every incoming message, and when the connection is established and broken. Its two arguments are the event type (see WebSocketMessageType) and the received data (a string, can be empty).
  • minreconnectwait and maxreconnectwait (in seconds) are optional values that limit the waiting time to try a reconnection to the server.


local function handleMessage(mt, data)
  if mt == WebSocketMessageType.OPEN then
    print("Connection open. Sending a message...")

  elseif mt == WebSocketMessageType.TEXT then
    print("Message recived: " .. data)

  elseif mt == WebSocketMessageType.CLOSE then
    print("Connection closed")

local ws = WebSocket{
    onreceive = handleMessage,
    url = "",
    deflate = false


local server = ws.url

Address of the server. Read-only, the url is specified when creating the websocket.


Try connecting to the server. After a successful connection, onreceive function will be called with message type WebSocketMessageType.OPEN. When the server or network breaks the connection, the client tries reconnecting automatically.


Disconnects from the server. After a disconnect, onreceive function will be called with message type WebSocketMessageType.CLOSE.


WebSocket:sendText(str1, str2, ...)

Sends a text message to the server. If multiple strings are passed, they will be joined together.


WebSocket:sendBinary(bstr1, bstr2, ...)

Sends a binary message to the server. If multiple strings are passed, they will be joined together. Lua makes no distinction between character and byte strings, but the websocket protocol does label them.



Sends a very short ping message to the server. There's a limit to the length of data that can be sent. It's sometimes used to prevent the connection from timing out and closing. A standard compliant server will reply to every "ping" message with a "pong". Client pongs are sent automatically, and there's no need to control that.