Skip to content

Commit

Permalink
feat: add web support (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-virkus committed Jan 29, 2023
1 parent 7f55bd3 commit 9c216bb
Show file tree
Hide file tree
Showing 41 changed files with 316 additions and 386 deletions.
9 changes: 6 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@
"name": "chat_app",
"cwd": "example\\chat_app",
"request": "launch",
"type": "dart"
"type": "dart",
"args": ["--web-port", "3000"]
},
{
"name": "chat_app (profile mode)",
"cwd": "example\\chat_app",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
"flutterMode": "profile",
"args": ["--web-port", "3000"]
},
{
"name": "chat_app (release mode)",
"cwd": "example\\chat_app",
"request": "launch",
"type": "dart",
"flutterMode": "release"
"flutterMode": "release",
"args": ["--web-port", "3000"]
}
]
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ Check out [client SDK API specification](https://centrifugal.dev/docs/transports

The features implemented by this SDK can be found in [SDK feature matrix](https://centrifugal.dev/docs/transports/client_sdk#sdk-feature-matrix).

Note that connection headers can only be set on platforms that support `dart:io`. So notably headers are ignored on the web platform.

> **The latest `centrifuge-dart` is compatible only with the latest [Centrifugo](https://github.com/centrifugal/centrifugo) server (v4) and [Centrifuge](https://github.com/centrifugal/centrifuge) >= 0.25.0. For Centrifugo v2, Centrifugo v3 and Centrifuge < 0.25.0 you should use `centrifuge-dart` v0.8.0.**
## Example
## Examples

* `example\flutter_app` simple chat application
* `example\chat_app` one more chat example
* `example\console` simple console application
* `example\console_server_subs` demonstrates working with server-side subscriptions

## Usage in background

Expand Down
6 changes: 6 additions & 0 deletions example/chat_app/.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ migration:
- platform: android
create_revision: 135454af32477f815a7525073027a3ff9eff1bfd
base_revision: 135454af32477f815a7525073027a3ff9eff1bfd
- platform: ios
create_revision: 135454af32477f815a7525073027a3ff9eff1bfd
base_revision: 135454af32477f815a7525073027a3ff9eff1bfd
- platform: web
create_revision: 135454af32477f815a7525073027a3ff9eff1bfd
base_revision: 135454af32477f815a7525073027a3ff9eff1bfd

# User provided section

Expand Down
48 changes: 44 additions & 4 deletions example/chat_app/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
# Chat app example

By default it will run with the Centrifugo public demo instance. To run locally use this configuration for your server:
Before running this example make sure you created `chat` namespace in Centrifugo configuration and allowed publishing into channel - i.e. using config like this:

```json
{
...
"token_hmac_secret_key": "9c4a1a64-7479-4c2c-9895-14b0676c71d8",
"admin_password": "a15cfff3-b7b7-4261-87ad-c6825c0400b5",
"admin_secret": "3aaead2e-2d3d-48cb-b330-c54c44b4eac0",
"api_key": "cb0f41cd-5954-443d-8628-a4453592443c",
"allowed_origins": ["http://localhost:8000"],
"namespaces": [
{
"name": "chat",
"anonymous": false,
"publish": true,
"join_leave": true,
"presence": true
"presence": true,
"presence_stats": true,
"allow_publish_for_subscriber": true,
"allow_subscribe_for_client": true
}
]
}
```

Then change the server address in `conf.dart`
Call `centrifugo genconfig` to create a basic `config.json` at the first time.

When you use your own configuration, please re-generate the tokens registered in `lib/conf.dart`:
* Use `centrifugo gentoken --user dart` to generate the user's JWT token.
* Use `centrifugo gensubtoken --user dart --channel chat:index` to generate the user's subscription JWT token.


Run Centrifugo with the admin option, to later send messages to all subscribers:

```bash
centrifugo --admin
```


For testing purposes only, you can also run Centrifugo in insecure client mode, so that the validity of JWT tokens
are not checked:

```bash
centrifugo --client_insecure --admin
```

Now check the IP address if your system with `ipconfig` on Windows and `ip adds` on Unix-like systems and change the `serverAddr` variable in `lib/conf.dart` accordingly.

When the configuration is correct, you can launch the app on the platform of your choice. When running on web, ensure that you specify a port using the `--web-port` argument
and that you have whitelisted that port in your Centrifugo `config.json` file. E.g. `"allowed_origins": ["http://localhost:8000", "http://localhost:3000"]` when running
locally on port 3000: `flutter run -d chrome --web-port 3000`.

When you have started centrifugo with the `--admin` option, you can also open `http://localhost:8000/#/actions` to send a message to your console app with the
following settings:
* Method: Publish
* Channel: `chat:index`
* Data: `{"message": "hello world", "username": "admin"}`

Congratulations, you have a running centrifugo system and a Flutter app that connects to it!
2 changes: 1 addition & 1 deletion example/chat_app/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include: package:extra_pedantic/analysis_options.yaml
include: package:flutter_lints/flutter.yaml

analyzer:
strong-mode:
Expand Down
7 changes: 0 additions & 7 deletions example/chat_app/android-old/.gitignore

This file was deleted.

63 changes: 0 additions & 63 deletions example/chat_app/android-old/app/build.gradle

This file was deleted.

This file was deleted.

47 changes: 0 additions & 47 deletions example/chat_app/android-old/app/src/main/AndroidManifest.xml

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

18 changes: 0 additions & 18 deletions example/chat_app/android-old/app/src/main/res/values/styles.xml

This file was deleted.

This file was deleted.

31 changes: 0 additions & 31 deletions example/chat_app/android-old/build.gradle

This file was deleted.

4 changes: 0 additions & 4 deletions example/chat_app/android-old/gradle.properties

This file was deleted.

This file was deleted.

15 changes: 0 additions & 15 deletions example/chat_app/android-old/settings.gradle

This file was deleted.

Loading

0 comments on commit 9c216bb

Please sign in to comment.