-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
Changes from 3 commits
755efef
627c36a
6679167
594acdc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
``` | ||
|
||
Alternatively to running in insecure mode, you can regenerate the tokens in `lib/src/conf.dart` on your system. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
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); | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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 rungo 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.