Bolt is a network protocol to send and receive strongly typed data objects
Documentation 📝
For documentation about Bolt, see the docs section.
An example of Bolt can be found in the example directory.
Quick Start 🚀
Prerequisites 📝
In order to start using Bolt you must have the Dart SDK installed on your machine.
Installing 🧑💻
Add bolt
to your pubspec.yaml
:
# 📦 Install bolt from pub.dev
dart pub add bolt
Creating a shared Data Object 💿
Create a shared DataObject
for the client and server:
class Ping extends DataObject {
const Ping(this.timestamp);
final int timestamp;
@override
List<Object?> get props => [timestamp];
static void register(BoltRegistry registry) {
registry.registerObject(
100,
DataResolver<Ping>(Ping.new, [
Argument.positional<Ping, int>((d) => d.timestamp, type: uint32),
]),
);
}
}
Creating a Server 🏁
Define a server, register the data object and listen to messages:
class ExampleServer extends BoltServer {
ExampleServer(super.address, {required super.bindings}) {
Ping.register(registry);
on(_onPinged);
}
void _onPinged(Message<Ping> message) {
// Do something on ping ...
}
@override
Future<bool> verifyAuth(Connection connection, String token) async {
return token == 'super_secure_token';
}
}
Creating a Client ✨
Define the client, register the data object and implement the onConnected
method:
class ExampleClient extends BoltClient {
ExampleClient(super.address, {super.server, required super.binding}) {
Ping.register(registry);
}
@override
void onConnected() {
send(Ping(DateTime.now().millisecondsSinceEpoch));
}
}