|
| 1 | +# HulypulseClient |
| 2 | + |
| 3 | +A TypeScript/Node.js client for the Hulypulse WebSocket server. |
| 4 | +Supports automatic reconnection, request–response correlation, `get` / `put` / `delete`, and subscriptions. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +### Main Methods |
| 9 | + |
| 10 | +## put(key: string, data: string, TTL?: number): Promise<boolean> |
| 11 | + |
| 12 | +Stores a value under a key. |
| 13 | + |
| 14 | + TTL (optional) — time-to-live in seconds. |
| 15 | + |
| 16 | + Resolves with true if the operation succeeded. |
| 17 | + |
| 18 | +await client.put("workspace/users/123", "Alice", 60) → true |
| 19 | + |
| 20 | +## get(key: string): Promise<any | false> |
| 21 | + |
| 22 | +Retrieves the value for a key. |
| 23 | + |
| 24 | + Resolves with the value if found. |
| 25 | + Resolves with false if the key does not exist. |
| 26 | + |
| 27 | +const value = await client.get("workspace/users/123") |
| 28 | +if (value) { |
| 29 | + console.log("User data:", value) |
| 30 | +} else { |
| 31 | + console.log("User not found") |
| 32 | +} |
| 33 | + |
| 34 | +## get_full(key: string): Promise<{data, etag, expires_at} | false> |
| 35 | + |
| 36 | +Retrieves the full record: |
| 37 | + |
| 38 | + data — stored value, |
| 39 | + etag — data identifier, |
| 40 | + expires_at — expiration in seconds. |
| 41 | + |
| 42 | +const full = await client.get_full("workspace/users/123") |
| 43 | +if (full) { |
| 44 | + console.log(full.data, full.etag, full.expires_at) |
| 45 | +} |
| 46 | + |
| 47 | +## delete(key: string): Promise<boolean> |
| 48 | + |
| 49 | +Deletes a key. |
| 50 | + |
| 51 | + Resolves with true if the key was deleted. |
| 52 | + Resolves with false if the key was not found. |
| 53 | + |
| 54 | +const deleted = await client.delete("workspace/users/123") |
| 55 | +console.log(deleted ? "Deleted" : "Not found") |
| 56 | + |
| 57 | +## subscribe(key: string, callback: (msg, key, index) => void): Promise<boolean> |
| 58 | + |
| 59 | +Subscribes to updates for a key (or prefix). |
| 60 | + |
| 61 | + The callback is invoked on every event: Set, Del, Expired |
| 62 | + |
| 63 | + Resolves with true if a new subscription was created. |
| 64 | + Resolves with false if the callback was already subscribed. |
| 65 | + |
| 66 | +const cb = (msg, key, index) => { |
| 67 | + if( msg.message === 'Expired' ) console.log(`${msg.key} was expired`) |
| 68 | +} |
| 69 | + |
| 70 | +await client.subscribe("workspace/users/", cb) |
| 71 | +// Now cb will be called when any key starting with "workspace/users/" changes |
| 72 | + |
| 73 | +## unsubscribe(key: string, callback: Callback): Promise<boolean> |
| 74 | + |
| 75 | +Unsubscribes a specific callback. |
| 76 | + |
| 77 | + Resolves with true if the callback was removed (and if it was the last one, the server gets an unsub message). |
| 78 | + Resolves with false if the callback was not found. |
| 79 | + |
| 80 | +await client.unsubscribe("workspace/users/", cb) |
| 81 | + |
| 82 | +## send(message: any): Promise<any> |
| 83 | + |
| 84 | +Low-level method to send a raw message. |
| 85 | + |
| 86 | + Automatically attaches a correlation id. |
| 87 | + Resolves when a response with the same correlation is received. |
| 88 | + |
| 89 | +const reply = await client.send({ type: "get", key: "workspace/users/123" }) |
| 90 | +console.log("Raw reply:", reply) |
| 91 | + |
| 92 | +## Reconnection |
| 93 | + |
| 94 | + If the connection drops, the client automatically reconnects. |
| 95 | + All active subscriptions are re-sent to the server after reconnect. |
| 96 | + |
| 97 | +## Closing |
| 98 | + |
| 99 | +The client supports both manual closing and the new using syntax (TypeScript 5.2+). |
| 100 | + |
| 101 | +client[Symbol.dispose]() // closes the connection |
| 102 | + |
| 103 | +or, if needed internally: |
| 104 | + |
| 105 | +(client as any).close() |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## Usage Example |
| 110 | + |
| 111 | +```ts |
| 112 | +import { HulypulseClient } from "./hulypulse_client.js" |
| 113 | + |
| 114 | +async function main() { |
| 115 | + // connect |
| 116 | + const client = await HulypulseClient.connect("wss://hulypulse_mem.lleo.me/ws") |
| 117 | + |
| 118 | + // subscribe to updates |
| 119 | + const cb = (msg, key, index) => { |
| 120 | + console.log("Update for", key, ":", msg) |
| 121 | + } |
| 122 | + await client.subscribe("workspace/users/", cb) |
| 123 | + |
| 124 | + // put value |
| 125 | + await client.put("workspace/users/123", JSON.stringify({ name: "Alice" }), 5) |
| 126 | + |
| 127 | + // get value |
| 128 | + const value = await client.get("workspace/users/123") |
| 129 | + console.log("Fetched:", value) |
| 130 | + |
| 131 | + // get full record |
| 132 | + const full = await client.get_full("workspace/users/123") |
| 133 | + if (full) { |
| 134 | + console.log(full.data, full.etag, full.expires_at) |
| 135 | + } |
| 136 | + |
| 137 | + // delete key |
| 138 | + const deleted = await client.delete("workspace/users/123") |
| 139 | + console.log(deleted ? "Deleted" : "Not found") |
| 140 | + |
| 141 | + // unsubscribe |
| 142 | + await client.unsubscribe("workspace/users/", cb) |
| 143 | + |
| 144 | + // low-level send |
| 145 | + const reply = await client.send({ type: "sublist" }) |
| 146 | + console.log("My sublists:", reply) |
| 147 | + |
| 148 | + // dispose |
| 149 | + client[Symbol.dispose]() |
| 150 | +} |
| 151 | + |
| 152 | +main() |
0 commit comments