# AgentLink SDK WASM
WebAssembly bindings for AgentLink SDK, enabling usage in browsers and Node.js.
## Building
```bash
# Build the WASM target
cargo build -p agentlink-wasm --target wasm32-unknown-unknown --release
# Generate JavaScript/TypeScript bindings
wasm-bindgen target/wasm32-unknown-unknown/release/agentlink_wasm.wasm \
--out-dir pkg \
--target web \
--typescript
```
## Usage
### In a Web Application
```html
<!DOCTYPE html>
<html>
<head>
<script type="module">
import init, { JsHttpClient, JsMqttClient, getVersion, init as sdkInit } from './pkg/agentlink_wasm.js';
async function main() {
await init();
sdkInit();
console.log('SDK Version:', getVersion());
const httpClient = new JsHttpClient('https://api.example.com');
httpClient.setAuthToken('your-auth-token');
try {
const response = await httpClient.get('/users/me');
console.log('User:', response);
} catch (e) {
console.error('Error:', e);
}
const mqttClient = new JsMqttClient();
mqttClient.onEvent((event) => {
console.log('MQTT Event:', event);
});
await mqttClient.connect('wss://mqtt.example.com/mqtt', 'client-1', 'username');
await mqttClient.subscribe('chat/room/1', 1);
const payload = new TextEncoder().encode('Hello, World!');
await mqttClient.publish('chat/room/1', payload, 1);
}
main();
</script>
</head>
<body>
<h1>AgentLink SDK WASM Demo</h1>
</body>
</html>
```
### In Node.js
Use `--target bundler` or `--target nodejs` when running `wasm-bindgen`:
```bash
wasm-bindgen target/wasm32-unknown-unknown/release/agentlink_wasm.wasm \
--out-dir pkg-node \
--target nodejs
```
```javascript
const { JsHttpClient, JsMqttClient, getVersion } = require('./pkg-node/agentlink_wasm.js');
console.log('SDK Version:', getVersion());
const client = new JsHttpClient('https://api.example.com');
// ... use the client
```
## API
### JsHttpClient
- `new JsHttpClient(baseUrl: string)` - Create a new HTTP client
- `setAuthToken(token: string)` - Set authentication token
- `getAuthToken(): string | undefined` - Get current auth token
- `get(path: string): Promise<any>` - Perform GET request
- `post(path: string, body: any): Promise<any>` - Perform POST request
- `put(path: string, body: any): Promise<any>` - Perform PUT request
- `delete(path: string): Promise<any>` - Perform DELETE request
### JsMqttClient
- `new JsMqttClient()` - Create a new MQTT client
- `connect(brokerUrl: string, clientId: string, username?: string): Promise<void>` - Connect to broker
- `disconnect(): Promise<void>` - Disconnect from broker
- `subscribe(topic: string, qos: number): Promise<void>` - Subscribe to topic
- `unsubscribe(topic: string): Promise<void>` - Unsubscribe from topic
- `publish(topic: string, payload: Uint8Array, qos: number): Promise<void>` - Publish message
- `getConnectionState(): string` - Get connection state
- `onEvent(callback: Function)` - Set event callback
### Functions
- `init()` - Initialize the SDK (sets up panic hook and logger)
- `getVersion(): string` - Get SDK version
- `loginWithEmailCode(baseUrl: string, email: string, code: string): Promise<any>` - Login helper
## Features
- ✅ HTTP Client using Fetch API
- ✅ MQTT Client using WebSocket
- ✅ TypeScript definitions included
- ✅ Async/Promise-based API
- ✅ Event callbacks for MQTT
## Notes
- The MQTT client uses WebSocket connections (ws:// or wss://)
- MQTT URLs are automatically converted: `mqtt://` → `ws://`, `mqtts://` → `wss://`
- The SDK requires a modern browser with WebAssembly support