Welcome to the hawki-client-example! 👋
This project is a simple yet functional chat application designed to demonstrate how to integrate a web app with the HAWKI real-time communication service. It serves as a practical, hands-on guide for developers looking to build on the HAWKI platform.
It features a basic PHP backend for handling authentication and a vanilla JavaScript frontend that acts as a Single Page Application (SPA). The frontend leverages the @hawk-hhg/hawki-client library to manage all data and communication, showcasing a powerful reactive state model that automatically updates the UI when data changes.
- 🚀 Demonstrates the core functionality of the
@hawk-hhg/hawki-clientlibrary. - 🌐 Simple PHP backend for secure API configuration and user authentication.
- 💻 Lightweight, framework-less vanilla JavaScript Single Page Application (SPA).
- ✨ Reactive state model for seamless, automatic UI updates.
- 💬 Real-time chat functionality powered by HAWKI.
- 🐳 Pre-configured Docker environment managed by a simple
bin/envhelper script for a consistent and easy setup.
This guide will walk you through setting up and running the project on your local machine.
Before you begin, ensure you have the following tools installed:
- Git
- Docker and Docker Compose
- A running HAWKI instance: This example needs a HAWKI backend to connect to.
- An SSH key configured with your GitHub account
Open your terminal, clone the repository, and navigate into the project directory.
git clone [email protected]:hawk-digital-environments/hawki-client-example.git
cd hawki-client-exampleFirst, create a local environment file by copying the provided template. This file will hold your secret keys and connection URLs.
cp .env.tpl .envYou will populate this .env file in the next step.
For this example to communicate with HAWKI, you must register it as an "external app" within your HAWKI instance.
-
Enable External Apps in HAWKI: In your HAWKI instance's environment configuration (e.g., its
.envfile), ensure the following variables are set totrue. You may need to restart your HAWKI services for this to take effect.ALLOW_EXTERNAL_COMMUNICATION=true ALLOW_EXTERNAL_APPS=true -
Run the Creation Command: In your HAWKI project's terminal, run the command to create a new external app.
# If using the provided HAWKI docker-compose environment bin/env artisan ext-app:create # Or, if running PHP natively php artisan ext-app:create
-
Answer the Prompts: The command-line tool will ask for some information. Use the following:
- Name:
HAWKI Client Example - URL of external app:
http://localhost(or the URL where you will run this example) - Redirect URL:
http://localhost(same as above)
Note: These URLs are used for browser redirects during the authentication flow. They only need to be accessible from the user's browser, not from the HAWKI server itself.
- Name:
-
Save Your Keys: The tool will output an API Token and a Private Key.
🚨 IMPORTANT: This is the only time you will see these keys! Copy them immediately and store them securely.
-
Update Your
.envfile: Paste the token and key into yourhawki-client-exampleproject's.envfile:HAWKI_API_TOKEN=<The API Token generated in step 4> HAWKI_PRIVATE_KEY=<The Private Key generated in step 4>
-
Ensure HAWKI Queue is Running: The
hawki-clientrequires a running WebSocket connection (Reverb), which depends on the queue worker. Make sure the queue worker is active in your HAWKI instance.# If using the provided HAWKI docker-compose environment bin/env dev # Or, run the queue worker directly php artisan queue:work
Next, you need to tell the example app how to reach your HAWKI instance by setting the HAWKI_URL in the .env file.
Option A: Connecting to the official HAWKI Docker setup (Recommended)
If you have cloned the main HAWKI repository and are running it via Docker Compose, you can link it with this example project. NOTE: Currently you need to check out the feature/external-chat branch of HAWKI for this to work.
- The HAWKI project opens a Docker network called
hawki_hawk_netby default. You can connect this example project to that network. The easiest way to do this is look in the root of the example project, where you can find adocker-compose.hawki-local.ymlfile. Simply copy this file todocker-compose.override.yml:cp docker-compose.hawki-local.yml docker-compose.override.yml
- Ensure your HAWKI instance is running:
cd /path/to/your/hawki/project bin/env up -d - In this example's
.envfile, theHAWKI_URLis already set to connect to the HAWKI instance inside the Docker by pointing to:https://hawki.hawk.docker. So you only need to need to setHAWKI_API_TOKENandHAWKI_PRIVATE_KEYas described in Step 3. - Restart the example's Docker containers to apply the network changes:
bin/env down bin/env up
Option B: Connecting to HAWKI running on your local machine
If you are running a local HAWKI instance directly on your host machine (e.g., at http://localhost), the Docker container for this example needs a special address to reach it.
- In this project's
.envfile, set theHAWKI_URLas follows:This special DNS name inside Docker resolves to your host machine's IP address.HAWKI_URL=http://host.docker.internal
This project uses a handy bin/env script to simplify Docker management.
-
(Optional, but Recommended) One-Time Install: The
bin/envtool has an optionalinstallcommand that configures a local domain (hawki-client-example.dev.local) and generates trusted SSL certificates. This allows you to access the app via HTTPS.# This may prompt for your administrator (sudo) password ./bin/env installYou can use the project without this step, but it provides a smoother development experience.
-
Start the Services: Use the
upcommand to build the Docker images and start the containers.bin/env up
This will start PHP and Nginx containers in the background and install Composer dependencies.
The application is now running!
- If you ran
bin/env install, simply use theopencommand:bin/env open
- If you skipped the
installstep, access the application at http://localhost.
You should see a login screen. Use any of the mock user credentials to log in and start exploring the chat!
The bin/env helper is your primary tool for managing the project. Here are the most common commands:
Click to see common bin/env commands
| Command | Description |
|---|---|
bin/env up |
Starts the Docker containers in the background. |
bin/env down |
Stops and removes the running Docker containers. |
bin/env logs |
Shows the logs for all services. Use bin/env logs -f to follow in real-time. |
bin/env ssh |
Opens a bash shell inside the main PHP service container. |
bin/env composer ... |
Runs a Composer command (e.g., install, update) inside the appropriate container. |
bin/env open |
Opens the project's URL in your browser (only if bin/env install was used). |
bin/env clean |
Destructive! Stops and removes all containers, networks, volumes, and images associated with the project. |
By default, this example automatically includes the latest released version of the @hawk-hhg/hawki-client library from npm
using the unpkg CDN. However, if you are developing the hawki-client library itself and want to test changes locally,
you can do so by following these steps:
Open your .env file and set the HAWKI_CLIENT_LIB_DIR variable to point to the local path of your hawki-client library:
HAWKI_CLIENT_LIB_DIR=/path/to/your/local/hawki-client/distThe target MUST be the
distdirectory inside your localhawki-clientproject, as it contains the built JavaScript files.
And that is it, really. When you start the example project using bin/env up, it will now use your local version of the library instead of fetching it from the CDN. To learn more about how this works, check the index.html file.
For a more in-depth guide and explanation of the concepts used in this project, check out the full documentation: