Learn how to use MQTT to connect Lua applications to IoT devices with this practical guide covering pub/sub patterns, QoS levels, and real-world code examples.
Guides
Guides
In-depth guides for Lua developers.
- Lua for MQTT and IoT Devices
- Cross-Platform Scripting with Lua
Write Lua scripts that run on Windows, macOS, and Linux. Covers paths, line endings, executable discovery, and platform detection.
- Dependency Management Patterns in Lua
Manage Lua dependencies with luarocks, submodule imports, and inline bundles. Find the right approach for your project size and deployment target.
- Event Systems and Message Passing in Lua
Build a simple event system in plain Lua. Covers callbacks, publish-subscribe, message queues, and integrating with game loops or OpenResty request handling.
- Logging Patterns and Frameworks in Lua
Add timestamps, log levels, and output to files with plain Lua. Then level up with LuaLogging or build your own structured logger.
- Designing Plugin Architectures in Lua
Build extensible Lua applications with plugin systems.Coyer plugins as modules, register hooks, handle lifecycle events, and isolate plugin code safely.
- Building Template Engines in Lua
Build template engines in Lua from scratch. Covers string interpolation, pattern-based templates, output buffering, and when to reach for LPeg.
- Finite Automata and Lexers in Lua
Build tokenizers and lexers in Lua with deterministic finite automata. Learn how DFAs work, recognize token patterns, and explore Lua patterns and LPeg.
- Scripting Redis with EVAL and Lua
Write atomic Lua scripts inside Redis with EVAL. Implement transaction-like patterns, atomic counters, and complex logic without multiple round-trips.
- Using SQLite from Lua
Connect Lua to SQLite using lsqlite3 or luasql. Execute queries, use prepared statements, handle transactions, and integrate with OpenResty.
- Implementing State Machines in Lua
Build state machines in Lua using tables, metatables, and coroutines. Covers the state pattern, event-driven transitions, and practical game and UI examples.
- Lua as a Configuration Language
Use Lua files as configuration. Return tables from .lua files, load them with loadfile, and validate settings with metatables.
- Lua-C Interop: A Practical Guide
Connect Lua and C code using the official C API. Register C functions for Lua, call Lua from C programs, and move data across the language boundary safely.
- Writing World of Warcraft Addons in Lua
Build a World of Warcraft addon with Lua. Learn the addon structure, event-driven model, saved variables, chat commands, and the WoW Lua API.
- Building Domain-Specific Languages in Lua
Build domain-specific languages in Lua with closures, metatables, and tables. Design fluent APIs for configuration, validation, and game scripting.
- Functional Programming Patterns in Lua
Apply functional ideas in Lua: higher-order functions, composition, pipelines, and immutability patterns without abandoning Lua's table-based style.
- Memory Optimization Techniques
Reduce memory usage in Lua programs by fixing leaks, reusing objects, and understanding how Lua allocates and collects memory.
- Profiling Lua Code for Bottlenecks
Learn how to find performance bottlenecks in Lua programs using manual timing, debug hooks, and LuaJIT's built-in profiler.
- Working with Binary Data in Lua 5.3+
Serialize and deserialize binary data using string.pack, unpack, and packsize. Covers format specifiers, endianness, and common pitfalls.
- LuaJIT FFI: Calling C from Lua
Learn how LuaJIT's FFI library lets you call C functions and use C data structures directly from Lua without writing extension modules.
- Parsing with LPeg and re
Parse structured text using LPeg, a PEG-based parsing library that ships with Lua 5.2+, and its companion re module.
- TCP and HTTP with LuaSocket
Learn to use LuaSocket for TCP client/server sockets, HTTP requests, URL parsing, and common networking patterns in Lua.
- Parsing and Generating JSON in Lua
Learn how to parse and generate JSON in Lua using dkjson, cjson, and json.lua with practical examples and common gotchas.
- Lua Performance Tips and Benchmarking
Practical techniques to write faster Lua: table pre-allocation, local caching, table.concat, and correct benchmarking.
- Serializing Lua Tables to Disk
Learn how to serialize Lua tables to disk using built-in approaches, json.lua, MessagePack, and the Lua C API.
- Sorting Tables: Comparators and Stability
Learn how table.sort() works, how to write custom comparators, and why Lua's sort is not stable.
- Closures and Upvalues in Practice
Learn how Lua closures capture variables from enclosing scopes, how upvalues work as live references, and practical patterns for stateful functions.
- Error Handling Patterns and Best Practices
Learn how to handle errors gracefully in Lua using pcall, xpcall, and patterns that keep your programs running.
- The debug Library: Inspection and Hooks
Explore Lua's debug library for inspecting stack frames, local variables, and upvalues at runtime, plus hooks for tracing and profiling code execution.
- Lua's Garbage Collector Explained
Learn how Lua's automatic garbage collector works, including the tri-color algorithm, collectgarbage() API, weak tables, and performance tuning.
- Writing Custom Iterators in Lua
Build custom iterators in Lua with closures, factory functions, and coroutines. Practical examples included.
- Sandboxing Lua for Safe Execution
Learn to safely execute untrusted Lua code. Control globals, block dangerous functions, and enforce CPU and memory limits.
- Lua String Patterns vs Regular Expressions
Lua string patterns look like regex but aren't. Learn the key differences, unique features, and the gotchas that'll trip you up.
- Testing Lua Code with Busted
Practical patterns for testing Lua with Busted — running specific tests, structuring test suites, isolating state, and integrating with CI pipelines.
- Weak Tables and Their Uses
Learn how weak tables let you hold references to objects without preventing garbage collection, with coverage of all three modes and practical use cases.
- Metatables and Metamethods in Lua
Learn how metatables let you customize table behavior, implement operator overloading, and build OOP patterns in Lua.
- Understanding Lua Environments and _ENV
Learn how Lua uses _ENV as an upvalue for global variable lookups, how to load code with custom environments, and how environments enable sandboxing.
- Filesystem Operations with LuaFileSystem
Guide to LuaFileSystem (lfs): working with directories, file attributes, timestamps, and file locking on Linux, macOS, and Windows.
- Managing Packages with LuaRocks
LuaRocks is the package manager for Lua. Learn to install, search, list, and remove packages from luarocks.org repository, and how to publish your own rocks.
- Penlight: A Batteries-Included Library
Penlight extends Lua's standard library with utilities for tables, strings, files, paths, and more. Learn the most useful modules with practical examples.
- MoonScript: CoffeeScript for Lua
MoonScript compiles to Lua, adding cleaner syntax, classes, arrow functions, and table comprehensions that make code easier to read and write.
- Teal: Typed Lua That Compiles to Lua
Teal is a typed dialect of Lua that compiles to plain Lua. Add type annotations to catch bugs at compile time, then run on any Lua runtime.