Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add web support #73

Merged
merged 4 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]
}
]
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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
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
32 changes: 29 additions & 3 deletions example/chat_app/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
# 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
{
...
"allowed_origins": ["http://localhost:8000", "http://localhost:3000"],
"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.

Also run Centrifugo in insecure client mode so it does not expect JWT token from client:

```bash
./centrifugo --config config.json --client_insecure --admin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid suggesting client_insecure mode in examples, unfortunately this often leads to using client_insecure mode in production

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, should I add the server config that matches the tokens in the code then?
Is it correct that a token generated with centrifugo gentoken do not expire?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They expire (168h by default for gentoken), I think you can run go run main.go gentoken -u dart -t 604800000 - so token will be valid ~19 years - should be enough for examples. But just need to emphasise that in reality token TTL should be somewhat more reasonable - like 10-120 mins or so.

```

Alternatively to running in insecure mode, you can regenerate the tokens in `lib/src/conf.dart` on your system.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think this should be default approach here. In general, examples may contain non-expiring tokens generated for the secret key used in the example. Would be awesome to mention that secret key should be changed and token expiration is recommended to have.


Now check the IP address if your system with `ipconfig` on Windows and `ip adds` on Unix-like systems and change the serverAddr 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!
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.

22 changes: 22 additions & 0 deletions lib/src/channel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

/// Connects the web socket channel
WebSocketChannel connect(
Uri uri, {
Iterable<String>? protocols,
Map<String, dynamic>? headers,
}) =>
IOWebSocketChannel.connect(
uri,
protocols: protocols,
headers: headers,
);

/// Extends the web socket channel
extension WebSocketChannelExtension on WebSocketChannel {
/// Sends the given binary [data]
void sendData(List<int> data) {
sink.add(data);
}
}
Loading