Coronium WebSockets /

Filename Size Date modified Message
4.5 KB

Coronium WebSocket Client

WebSocket Instances

Visit for installation information.

Testing WebSockets

To test the WebSockets in Coronium, Download the WebSocket example source files here.

Example 00_echo :

  1. From the 00_echo/_websocket example folder, upload the main.lua into the lua/_websocket folder.

  2. Replace the coronium_host variable near the top in 00_echo/ws.html with your Coronium instance host address.

  3. Open 00_echo/ws.html in a browser -- Google Chrome recommended. If using Chrome, open up the Developer Tools for easier debugging by going to your Chrome options and selecting Tools > JavaScript Console and setting console_output=true in ws.html.

  4. This test is just a simple echo function. Put a string or JSON structure in the Enter data field.

  5. Press [Return] and you should receive an echoed value printed to the screen.

Example 01_basic : A simple addition function. See the 01_basic folder.

Example 02_mongo:

For the MongoDB example, the setup is similar to the first example. From the 02_mongo/_websocket example folder, upload the main.lua into the lua/_websocket folder.

Replace the coronium_host variable near the top in 02_mongo/ws.html with your Coronium instance host address.

In this example we can add some records to a MongoDB store, and get back a listing to display. Start by adding a couple of users. In the Enter data field do the following:


{"action":"add", "username":"Chris"}

and press [Return]. The message "added" should be output on the screen.


{"action":"add", "username":"Daniel"}

and press [Return]. The message "added" should be output on the screen.

We now have 2 records in the MongoDB store. Now let's list them on the screen as well:



and press [Return]. A list of the users you added should be output on the screen.

Study the example files to learn more how to utilize WebSockets with Coronium.

Programming Coronium WebSockets

The WebSockets bootstrap file is the main.lua file located in the lua/_websockets folder on your Coronium instance. It is pre-populated with the echo example, though WebSockets are disabled by default.

There are two main event methods for WebSockets, onData and onError.

onData( data_in, output, close )

The onData handler will be called when the WebSocket client sends data to the Coronium instance. The onData handler has 3 incoming parameters, they are as follows:

data_in: This is the incoming data from the WebSocket client. It will be a plain, or JSON formatted string. To convert this string to a Lua table use:

-- JSON to Lua table
local tbl = coronium.json.encode( data_in )

output: This is the method you use to send data back to the WebSocket client. Like other Coronium returns, you need to pack the data in a Coronium answer table:

-- Return data to the WebSocket client
output( coronium.answer( { username = "Chris" } )

close: This is a method that can be used to close the WebSocket connection. If you call this method, the WebSocket client will be disconnected and you will no longer receive, or be able to send, data to this client.

-- Close the connection

onError( error_msg )

The onError method will be called when an error occurs involving the WebSocket client. In almost all cases, the client will be disconnected when an error occurs. Common errors are connection issues, and malformed data.

-- Log error
coronium.log( error_msg )

Corona SDK WebSockets

WebSocket support is commonly used in a web browser with the JQuery Websocket, but if you want to use a Corona SDK websocket client, please see DMC WebSockets at

It's generally easier to use the dedicated Corona SDK Coronium module to interface with your Coronium instance if you're using Corona SDK.


Please visit for more information on Coronium WebSocket support and tips.