6.6 KiB
Kosmi Client - Standalone CLI Chat Client
A simple, standalone command-line client for Kosmi that demonstrates WebSocket communication, GraphQL operations, and real-time chat functionality.
Features
- ✅ Manual JWT token authentication
- ✅ Email/password authentication with browser automation
- ✅ Interactive CLI for sending/receiving messages
- ✅ Real-time message subscription
- ✅ Graceful shutdown (Ctrl+C)
Prerequisites
- Go 1.21 or higher
- A Kosmi account (for authenticated access)
- JWT token from your browser session
Installation
cd cmd/kosmi-client
go mod download
go build
Getting Your JWT Token
To use this client, you need to extract your JWT token from the Kosmi web application:
Method 1: Browser DevTools (Recommended)
- Open https://app.kosmi.io in your browser
- Log in to your account
- Open Developer Tools (F12 or Right-click → Inspect)
- Go to the Console tab
- Type the following command and press Enter:
localStorage.getItem('token') - Copy the token (it will be a long string starting with
eyJ...)
Method 2: Network Tab
- Open https://app.kosmi.io in your browser
- Open Developer Tools (F12)
- Go to the Network tab
- Filter by "WS" (WebSocket)
- Look for the connection to
engine.kosmi.io - Click on it and view the Messages tab
- Find the
connection_initmessage - Copy the token from the payload
Usage
Basic Usage
Option 1: Email/Password (Recommended)
./kosmi-client --email "your@email.com" --password "yourpassword" --room "@hyperspaceout"
Option 2: Manual Token
./kosmi-client --token "YOUR_JWT_TOKEN" --room "@hyperspaceout"
Command-Line Options
| Flag | Description | Required | Example |
|---|---|---|---|
--token |
JWT authentication token | Yes* | --token "eyJ..." |
--room |
Room ID to join | Yes | --room "@hyperspaceout" |
--email |
Email for login | Yes* | --email "user@example.com" |
--password |
Password for login | Yes* | --password "secret" |
* Either --token OR both --email and --password are required
Interactive Commands
Once connected, you can use these commands:
- Send a message: Just type your message and press Enter
/help- Show available commands/quitor/exit- Exit the client- Ctrl+C - Graceful shutdown
Example Session
$ ./kosmi-client --email "email@email.com" --password "password" --room "@hyperspaceout"
Connecting to Kosmi WebSocket...
Sending connection_init...
✓ Connection acknowledged
Querying current user...
✓ Logged in as: John Doe (@johndoe) [Anonymous: false]
Joining room: @hyperspaceout...
✓ Successfully joined room
Subscribing to new messages...
✓ Subscription active
Connection setup complete!
============================================================
Connected! Type messages and press Enter to send.
Press Ctrl+C to exit.
============================================================
> Hello, world!
> [14:23:45] Jane Smith: Hey there!
> How are you?
> [14:24:01] Jane Smith: I'm good, thanks!
> /quit
Exiting...
Closing Kosmi client...
✓ Client closed
Goodbye!
Architecture
The client consists of three main components:
1. auth.go - Authentication Management
GetToken()- Retrieves token (manual or via chromedp)loginWithChromedp()- Browser automation for email/password login
2. client.go - WebSocket Client
KosmiClient- Main client structConnect()- Establishes connection and performs GraphQL handshakeSendMessage()- Sends messages to the roomlistenForMessages()- Receives messages in real-timeClose()- Graceful shutdown
3. main.go - CLI Interface
- Command-line argument parsing
- Interactive message input/output
- Signal handling for graceful shutdown
GraphQL Operations
The client uses these minimal GraphQL operations:
- connection_init - Authenticate with JWT token
- ExtendedCurrentUserQuery - Get current user info
- JoinRoom - Join the specified room
- NewMessageSubscription - Subscribe to new messages
- SendMessage2 - Send messages to the room
Troubleshooting
"Authentication error: no authentication method provided"
You must provide either --token or both --email and --password.
"Connection error: failed to connect to WebSocket"
- Check your internet connection
- Verify the Kosmi service is accessible
- Ensure you're not behind a restrictive firewall
"expected connection_ack, got: connection_error"
Your JWT token is likely invalid or expired. Get a fresh token from your browser.
Messages not appearing
- Verify you've joined the correct room ID
- Check that the room exists and you have access to it
- Ensure your token has the necessary permissions
Helper Tools
cmd/get-kosmi-token - Standalone Token Extractor
A separate utility for extracting JWT tokens without running the full client:
cd cmd/get-kosmi-token
go run main.go --email "your@email.com" --password "yourpassword"
Options:
--email- Your Kosmi email--password- Your Kosmi password--headless- Run browser in headless mode (default: true)--verbose- Enable verbose logging
The token will be printed to stdout, making it easy to capture:
TOKEN=$(go run main.go --email "your@email.com" --password "yourpassword" 2>/dev/null)
./kosmi-client --token "$TOKEN" --room "@hyperspaceout"
Future Enhancements
Additional Features (Ideas)
- Room chat history retrieval (
RoomChatQuery) - Member join/leave notifications
- Typing indicators
- File/image upload support
- Multiple room support
- Persistent token storage
- Reconnection with exponential backoff
- Message editing and deletion
- Reactions and emojis
Development
Running from Source
go run . --token "YOUR_TOKEN" --room "@hyperspaceout"
Building
go build -o kosmi-client
Dependencies
github.com/gorilla/websocket- WebSocket client librarygithub.com/chromedp/chromedp- Browser automation (for future use)
License
This is a proof-of-concept demonstration client. Use responsibly and in accordance with Kosmi's Terms of Service.
Contributing
This is a standalone proof of concept. Feel free to fork and extend it for your own use cases!
Support
For issues related to:
- Kosmi service: Contact Kosmi support
- This client: Check the code comments and implementation details
Note: This client is not officially affiliated with or endorsed by Kosmi. It's an independent implementation for educational and development purposes.