reachlo 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +52 -0
  2. package/index.js +75 -0
  3. package/package.json +13 -0
package/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Reachlo SDK
2
+
3
+ **Real-time infrastructure for AI streaming.**
4
+
5
+ Reachlo is a lightweight, WebSocket-based real-time messaging layer designed for AI agents and modern web applications. It simplifies the complexity of real-time communication into a few lines of code.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install reachlo
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ### 1. Connect
16
+
17
+ Initialize the client with your API key.
18
+
19
+ ```javascript
20
+ import Reachlo from 'reachlo';
21
+
22
+ const client = new Reachlo('YOUR_API_KEY');
23
+
24
+ await client.connect();
25
+ ```
26
+
27
+ ### 2. Subscribe & Publish
28
+
29
+ Subscribe to channels to receive updates, and publish messages to them instantly.
30
+
31
+ ```javascript
32
+ const chat = client.channel('chat-room-1');
33
+
34
+ // Listen for incoming messages
35
+ chat.subscribe((msg) => {
36
+ console.log('New message:', msg);
37
+ });
38
+
39
+ // Send a message
40
+ chat.publish({ text: 'Hello world!', sender: 'User' });
41
+ ```
42
+
43
+ ## Why Reachlo?
44
+
45
+ - **Zero Config**: No complex server setup required.
46
+ - **WebSocket Native**: Built on top of `uWebSockets.js` for extreme performance.
47
+ - **Channel Routing**: Automatically routes messages to the correct channel listeners.
48
+ - **Authentication**: Simple API key-based auth handled transparently.
49
+
50
+ ## License
51
+
52
+ MIT
package/index.js ADDED
@@ -0,0 +1,75 @@
1
+ // index.js
2
+ class Reachlo {
3
+ constructor(apiKey, options = {}) {
4
+ this.apiKey = apiKey;
5
+ // Default to production URL if none provided
6
+ this.url = options.url || 'wss://your-app.fly.dev';
7
+ this.socket = null;
8
+ this.channels = new Map();
9
+ }
10
+
11
+ connect() {
12
+ return new Promise((resolve, reject) => {
13
+ const urlWithAuth = `${this.url}?api_key=${this.apiKey}`;
14
+ this.socket = new WebSocket(urlWithAuth);
15
+
16
+ this.socket.onopen = () => {
17
+ console.log("Reachlo: Connected");
18
+ resolve();
19
+ };
20
+
21
+ this.socket.onerror = (err) => reject(err);
22
+
23
+ this.socket.onmessage = (event) => {
24
+ try {
25
+ const payload = JSON.parse(event.data);
26
+ // Routes data to the specific channel instance
27
+ if (payload.channel && this.channels.has(payload.channel)) {
28
+ this.channels.get(payload.channel)._emit(payload.data);
29
+ }
30
+ } catch (e) {
31
+ console.error("Reachlo: Message parse error", e);
32
+ }
33
+ };
34
+ });
35
+ }
36
+
37
+ channel(channelName) {
38
+ if (!this.channels.has(channelName)) {
39
+ const channel = new ReachloChannel(channelName, this);
40
+ this.channels.set(channelName, channel);
41
+ this._send({ type: 'subscribe', channel: channelName });
42
+ return channel;
43
+ }
44
+ return this.channels.get(channelName);
45
+ }
46
+
47
+ _send(payload) {
48
+ if (this.socket && this.socket.readyState === WebSocket.OPEN) {
49
+ this.socket.send(JSON.stringify(payload));
50
+ }
51
+ }
52
+ }
53
+
54
+ class ReachloChannel {
55
+ constructor(name, client) {
56
+ this.name = name;
57
+ this.client = client;
58
+ this.callbacks = new Set();
59
+ }
60
+
61
+ subscribe(cb) {
62
+ this.callbacks.add(cb);
63
+ }
64
+
65
+ publish(data) {
66
+ this.client._send({ type: 'publish', channel: this.name, data });
67
+ }
68
+
69
+ _emit(data) {
70
+ // Sends the raw data to all registered listeners for this channel
71
+ this.callbacks.forEach(cb => cb(data));
72
+ }
73
+ }
74
+
75
+ export default Reachlo;
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "reachlo",
3
+ "version": "1.0.0",
4
+ "description": "Real-time infrastructure for AI streaming.",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "author": "Your Name",
8
+ "license": "MIT",
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "keywords": []
13
+ }