Skip to content

Commit

Permalink
Add info about centrifugo in example
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Jul 21, 2023
1 parent 6d6606b commit 279427f
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,7 @@ log.pana.json
# Test
.coverage/
/test/**/*.json
/test/.test_coverage.dart
/test/.test_coverage.dart

# Centifuge
config.json
126 changes: 126 additions & 0 deletions examples/console/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Example console chat app

## Getting started with Centrifugo

Before running this example make sure you created `chat` namespace in Centrifugo configuration and allowed publishing into channel.

1. Call `centrifugo genconfig` to create a basic `config.json` at the first time.

2. Update the `config.json` file with tokens and namespaces, e.g.:

```json
{
"token_hmac_secret_key": "<TOKEN HMAC SECRET KEY>",
"admin_password": "<ADMIN PASSWORD>",
"admin_secret": "<ADMIN SECRET>",
"api_key": "<API KEY>",
"allowed_origins": ["http://localhost:8000"],
"namespaces": [
{
"name": "chat",
"anonymous": false,
"publish": true,
"join_leave": true,
"presence": true,
"presence_stats": true,
"allow_publish_for_subscriber": true,
"allow_subscribe_for_client": true
}
]
}
```

3. When you use your own configuration, please re-generate the tokens registered in `example.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.

4. 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
```

5. 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 `example.dart` accordingly.

6. When the configuration is correct, you can launch the console app with `dart example.dart`.

7. 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 console app that connects to it!

## Centrifugo with Docker

### Configurate Centrifugo

First, you need to create a config file for Centrifugo. You can do this with Docker:

1. Generate config file:

Bash:

```bash
docker run -it --rm --volume ${PWD}:/centrifugo \
--name centrifugo centrifugo/centrifugo:latest centrifugo genconfig
```

PowerShell:

```powershell
docker run -it --rm --volume ${PWD}:/centrifugo `
--name centrifugo centrifugo/centrifugo:latest centrifugo genconfig
```

2. Generate user token
with `centrifugo gentoken --user dart` to generate the user's JWT token.
`centrifugo gensubtoken --user dart --channel chat:index` to generate the user's subscription JWT token.

Bash:

```bash
docker run -it --rm --volume ${PWD}/config.json:/centrifugo/config.json:ro \
--name centrifugo centrifugo/centrifugo:latest \
centrifugo gensubtoken --user dart --channel chat:index
```

PowerShell:

```powershell
docker run -it --rm --volume ${PWD}/config.json:/centrifugo/config.json:ro `
--name centrifugo centrifugo/centrifugo:latest `
centrifugo gensubtoken --user dart --channel chat:index
```

### Run Centrifugo

You can also run the example with Docker. First, build the image:

Bash:

```bash
docker run -d -it --rm --ulimit nofile=65536:65536 --port 8000:8000 \
--volume ${PWD}/config.json:/centrifugo/config.json:ro \
--name centrifugo centrifugo/centrifugo:latest centrifugo \
--client_insecure --admin --admin_insecure --log_level=debug
```

PowerShell:

```powershell
docker run -d -it --rm --ulimit nofile=65536:65536 --port 8000:8000 `
--volume ${PWD}/config.json:/centrifugo/config.json:ro `
--name centrifugo centrifugo/centrifugo:latest centrifugo `
--client_insecure --admin --admin_insecure --log_level=debug
```
10 changes: 10 additions & 0 deletions lib/src/client/centrifuge_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ abstract interface class ICentrifuge {
/// Stream of client states.
abstract final Stream<CentrifugeState> states;

/* abstract final Stream<Object> publications; */

/// Connect to the server.
/// [url] is a URL of endpoint.
Future<void> connect(String url);

// Send asynchronous message to a server. This method makes sense
// only when using Centrifuge library for Go on a server side. In Centrifugo
// asynchronous message handler does not exist.
//Future<void> send(List<int> data);

/// Publish data to the channel.
/* Future<PublishResult> publish(String channel, List<int> data); */

/// Disconnect from the server.
Future<void> disconnect();

Expand Down

0 comments on commit 279427f

Please sign in to comment.