Bolt is a network protocol written in Dart to send and receive strongly typed data objects. It is designed to be easy to use and to be as fast as possible.
Bolt is split into two parts, the BoltClient
and the BoltServer
. They both implement the BoltProtocol
, which handles settings up the connection, verifying the connection is secure and sending/receiving data objects.
Everything is abstracted away in these classes, this means that you can implement your own abstraction on top of Bolt by just extending from BoltClient
and BoltServer
.
Bolt works on the principal of shared code, this means that you write common code that is shared between both server and client.
Package | Pub |
---|---|
bolt | |
bolt_udp_binding | |
bolt_websocket_binding |
For documentation about Bolt, see the docs section.
An example of Bolt can be found in the example directory.
In order to start using Bolt you must have the Dart SDK installed on your machine.
Add bolt
to your pubspec.yaml
:
# 📦 Install bolt from pub.dev
dart pub add bolt
Create a shared Data Object 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),
]),
);
}
}
Define a server, register the data object and listen to messages:
class ExampleServer extends BoltServer {
ExampleServer(super.address) {
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';
}
}
Define the client, register the data object and implement the onConnected
method:
class ExampleClient extends BoltClient {
ExampleClient(super.address, {super.server}) {
Ping.register(registry);
}
@override
void onConnected() {
send(Ping(DateTime.now().millisecondsSinceEpoch));
}
}