Actix
Actix is a rust actor system framework.
- API Documentation (Development)
- API Documentation (Releases)
- Cargo package: actix
Actix is licensed under the Apache-2.0 license.
Features
- Typed messages (No
Any
type). Generic messages are allowed. - Async/Sync actors.
- Actor communication in a local/thread context.
- Using Futures for asynchronous message handling.
- Http support (actix-web)
- Actor supervision.
Usage
To use actix
, add this to your Cargo.toml
:
[]
= "0.3"
You may consider to check chat example.
Initialize the Actix
In order to use actix you first need to create an System
.
extern crate actix;
Actix uses tokio event loop.
System::new()
call creates new event loop and starts System
actor.
system.run()
starts tokio event loop and will finish once the System
actor
receives SystemExit
message.
Let's create simple Actor.
Implement an Actor
In order to define an actor you need to define a struct and have it implement
the Actor
trait.
extern crate actix;
use ;
;
Spawning a new actor is achieved via the start
and create
methods of
Actor
trait. It provides several different ways of creating actors, for details check docs.
You can implement started
, stopping
and stopped
mthods of Actor trait,
started
method get called when actor starts and stopping
when actor finishes.
Check api documentation
for more information on actor lifecycle.
Handle messages
An Actor communicates with another Actor by sending an messages. In actix all messages are typed.
Let's define simple Sum
message with two usize
parameters and actor which will
accept this message and return sum of those two numbers.
extern crate actix;
extern crate futures;
use ;
use *;
// this is our Message
;
// we have to define type of response for `Sum` message
// Actor definition
;
// now we need to define `MessageHandler` for `Sum` message.
All communications with actors go through Address
object. You can send
message
without waiting response or call
actor with specific message. ResponseType
trait defines response type for a message, Item
and Error
for value and error respectevily.
There are different types of addresses.
Address<A>
is an address
of an actor that runs in same arbiter (event loop). If actor is running in different
thread SyncAddress<A>
has to be used.
Actor state and subscription for specific message
If you noticed, methods of Actor
and Handler
traits accept &mut self
, so you are welcome to
store anything in an actor and mutate it whenever you need.
Address object requires actor type, but if we just want to send specific message to
an actor that can handle message, we can use Subscriber
interface. Let's create
new actor that uses Subscriber
, also this example will show how to use standard future objects.
extern crate actix;
extern crate tokio_core;
use Duration;
use *;
use signal;
;
// Actor definition
// message handler for Ping message
More information on signals handling is in signal module.
chat example
You may consider to check chat example. It provides basic example of networking client/server service.
fectl
You may consider to check fectl utility. It is written
with actix
and shows how to create networking application with relatevly complex interactions.
Contributing
All contribution are welcome, if you have a feature request don't hesitate to open an issue!