A safe scripting environment for actix with the Lua Programming Language:
- Each
LuaActoris an isolated Lua VM. - Communicate between actors with predefined message types:
String,Integer,Number,Boolean,Nil, andTable. - Asynchronous
sendbetween actors with Lua coroutine.
For more info about the "safety", check rlua's README.
A basic Lua actor
extern crate actix_lua;
use actix_lua::{LuaActorBuilder, LuaMessage};
fn main () {
let addr = LuaActorBuilder::new()
.on_handle_with_lua(r#"return ctx.msg + 42"#)
.build()
.unwrap()
.start();
let res = addr.send(LuaMessage:from(100));
// return: 142
}You can send messages to other actor asynchronously with ctx.send
struct Callback;
impl Actor for Callback {
type Context = Context<Self>;
}
impl Handler<LuaMessage> for Callback {
type Result = LuaMessage;
fn handle(&mut self, msg: LuaMessage, _ctx: &mut Context<Self>) -> Self::Result {
LuaMessage::String("hello".to_string())
}
}
let mut actor = LuaActorBuilder::new()
// create a new LuaActor from a lua script when the actor is started.
// send message to the newly created actor with `ctx.send`, block and wait for its response.
.on_started_with_lua(
r#"
local result = ctx.send("callback, "Hello")
print(result) -- print "hello"
"#).build()
.unwrap();
actor.add_recipients("callback", Callback.start().recipient());
actor.start();Add actix-lua to your Cargo.toml:
[dependencies]
actix-lua = "0.7"Check examples directory.
There's also a write-up about analyzing streaming data with actix-lua. link
Use LuaActor to integrate Lua scripts to your system with actor model.
In actor model, actors communicate with messages. LuaMessage is the only message type accepted by LuaActor:
LuaMessagecan be converted to/from primitive types withLuaMessage::from().- Lua types(e.g. number, table) will be convert to
LuaMessageautomatically.
Note: Avoid declaring global variables in your Lua script. It might conflict with future actix-lua update and break your program.
The message sent to Lua actor.
Send message msg to self.
Send message msg to self after specified period of time.
Send message msg to `recipient asynchronously and wait for response.
Equivalent to actix::Recipient.send.
Send message msg to recipient.
Equivalent to actix::Recipient.do_send.
Terminate actor execution.
The MIT License