Skip to content

Commit

Permalink
Merge pull request #55 from muzzammilshahid/mkdocs
Browse files Browse the repository at this point in the history
Add basic documentaion
  • Loading branch information
muzzammilshahid authored Jul 22, 2024
2 parents 024310e + 987c810 commit d6a9f35
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 12 deletions.
15 changes: 11 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# Overview
WAMP v2 Client for Dart.

For full documentation visit [XConnIO](https://www.xconn.io).
XConn provides a powerful and easy-to-use WAMP v2 client and router for Dart. It is designed to be
cross-platform, making it equally useful in both pure Dart applications and Flutter projects,
offering cross-platform compatibility across desktop, web, and mobile environments.

# Installation
For installation & usage [xconn-dart](https://xconn.io/xconn/dart/)
For examples and project code, visit the [GitHub repository](https://github.com/xconnio/xconn-dart).

For installation, see [Installation](https://docs.xconn.io/xconn/dart/installation) section.

For usage, see [Usage](https://docs.xconn.io/xconn/dart/usage) section.

For questions or issues, please visit our [GitHub repository](https://github.com/xconnio/xconn-dart)
and feel free to open an issue.
18 changes: 11 additions & 7 deletions docs/installation.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
# Installation
Use this package as a library

Depend on it.
Use this package as a library.

Run this command:
## Add Dependency

**With Dart**
To add this package to your project, run the appropriate command for your environment:

### With Dart

```shell
dart pub add xconn
```

**With Flutter**
### With Flutter

```shell
flutter pub add xconn
```

This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):

```yaml
dependencies:
xconn: ^0.1.0
```
Alternatively, your editor might support `dart pub get` or `flutter pub get`. Check the docs for your editor to learn more.
Alternatively, your editor might support `dart pub get` or `flutter pub get`. Check the docs for
your editor to learn more.

### Import it

Import it
Now in your Dart code, you can use:

```dart
Expand Down
205 changes: 205 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# Usage

XConn provides a versatile WAMP v2 client and router for Dart applications. Below are examples
demonstrating various functionalities:

## Client

### Creating a Client

To create a client and connect to a WAMP server:

```dart
import "package:xconn/xconn.dart";
void main() async {
var client = Client();
var session = await client.connect("ws://localhost:8080/ws", "realm1");
}
```

Once the session is established, you can perform WAMP actions. Below are examples of all 4 WAMP
operations:

### Subscribe to a topic

To subscribe to a topic and handle events:

```dart
void exampleSubscribe(Session session) async {
var subscription = await session.subscribe("io.xconn.example", eventHandler);
print("Subscribed to topic io.xconn.example");
}
void eventHandler(Event event) {
print("Received Event: args=${event.args}, kwargs=${event.kwargs}, details=${event.details}");
}
```

### Publish to a topic

To publish messages to a topic:

```dart
void examplePublish(Session session) async {
await session.publish("io.xconn.example", args: ["Hello World!", 100], kwargs: {"xconn": "dart"});
print("Published to topic io.xconn.example");
}
```

### Register a procedure

To register a procedure:

```dart
void exampleRegister(Session session) async {
var registration = await session.register("io.xconn.echo", invocationHandler);
print("Registered procedure io.xconn.echo");
}
Result invocationHandler(Invocation invocation) {
return Result(args: invocation.args, kwargs: invocation.kwargs, details: invocation.details);
}
```

### Call a procedure

To call a procedure:

```dart
void exampleCall(Session session) async {
var result = await session.call("io.xconn.echo", args: ["Hello World!"], kwargs: {"number": 100});
print("Call result: args=${result.args}, kwargs=${result.kwargs}, details=${result.details}");
}
```

### Authentication

Authentication is straightforward. Simply create the object of the desired authenticator and pass it
to the Client.

**Ticket Auth**

```dart
void main() async {
var ticketAuthenticator = TicketAuthenticator(ticket, authid);
var client = Client(authenticator: ticketAuthenticator);
var session = await client.connect("ws://localhost:8080/ws", "realm1");
}
```

**Challenge Response Auth**

```dart
void main() async {
var craAuthenticator = WAMPCRAAuthenticator(secret, authid);
var client = Client(authenticator: craAuthenticator);
var session = await client.connect("ws://localhost:8080/ws", "realm1");
}
```

**Cryptosign Auth**

```dart
void main() async {
var cryptosignAuthenticator = CryptoSignAuthenticator(privateKey, authid);
var client = Client(authenticator: cryptosignAuthenticator);
var session = await client.connect("ws://localhost:8080/ws", "realm1");
}
```

### Serializers

XConn supports various serializers for different data formats. To use, create an instance of your
chosen serializer and pass it to the client.

**JSON Serializer**

```dart
void main() async {
var jsonSerializer = JSONSerializer();
var client = Client(serializer: jsonSerializer);
var session = await client.connect("ws://localhost:8080/ws", "realm1");
}
```

**CBOR Serializer**

```dart
void main() async {
var cborSerializer = CBORSerializer();
var client = Client(serializer: cborSerializer);
var session = await client.connect("ws://localhost:8080/ws", "realm1");
}
```

**MsgPack Serializer**

```dart
void main() async {
var msgPackSerializer = MsgPackSerializer();
var client = Client(serializer: msgPackSerializer);
var session = await client.connect("ws://localhost:8080/ws", "realm1");
}
```

For more detailed examples or usage, refer to
the [examples](https://github.com/xconnio/xconn-dart/tree/main/examples) folder of the project.

## Server

### Setting Up a Basic Server

Setting up a basic server is straightforward:

```dart
import 'package:xconn/xconn.dart';
void main() async {
var router = Router()
..addRealm('realm1');
var server = Server(router);
await server.start('localhost', 8080);
}
```

### Setting Up Server with Authenticator

Here's a simple example of a server authenticator and how to pass it to the server:

```dart
class ServerAuthenticator extends IServerAuthenticator {
@override
Response authenticate(Request request) {
if (request is AnonymousRequest) {
// Handle anonymous request
} else if (request is TicketRequest) {
// Handle ticket request
} else if (request is WAMPCRARequest) {
// Handle wampcra request
} else if (request is CryptoSignRequest) {
// Handle cryptosign request
}
throw Exception("unknown authmethod");
}
@override
List<String> methods() {
return ["anonymous", "ticket", "wampcra", "cryptosign"];
}
}
void main() async {
var router = Router()
..addRealm('realm1');
var server = Server(router);
// Start the server with the custom authenticator
await server.start('localhost', 8080, authenticator: ServerAuthenticator());
}
```

For more advanced usage, such as integrating an authenticator, refer to the sample tool available
in the [bin](https://github.com/xconnio/xconn-dart/tree/main/bin/xconn) folder of the project.

1 change: 0 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ theme:
- navigation.instant
- navigation.tracks
- navigation.sections
- toc.integrate
- navigation.top
- search.suggest
- search.highlight
Expand Down

0 comments on commit d6a9f35

Please sign in to comment.