arifa-client 1.0.2 → 1.0.4
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.
- package/README.md +139 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,9 +3,146 @@
|
|
|
3
3
|
JavaScript/TypeScript client SDK for **Arifa Realtime Notification Service**.
|
|
4
4
|
Allows you to connect via WebSocket, listen to events, and send notifications using HTTP.
|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
## Installation
|
|
8
7
|
|
|
9
8
|
```bash
|
|
10
9
|
npm install arifa-client
|
|
11
|
-
```
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
or using Yarn:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
yarn add arifa-client
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
#### Import and Initialize
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import { ArifaClient } from "arifa-client";
|
|
24
|
+
|
|
25
|
+
const client = new ArifaClient({
|
|
26
|
+
apiKey: "YOUR_API_KEY",
|
|
27
|
+
client: "web", // "web" or "mobile"
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
#### Listen to Connection State
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
client.onConnectionChange((state) => {
|
|
35
|
+
console.log("Connection state:", state); // "connected" or "disconnected"
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### Subscribe Recipient
|
|
40
|
+
|
|
41
|
+
Note: `Recipient must be UUID`
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
const sub = client.subscribe("RECIPIENT_UUID");
|
|
45
|
+
|
|
46
|
+
// Listen for events
|
|
47
|
+
sub.listen((event) => {
|
|
48
|
+
console.log("Received event:", event);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Unsubscribe when done
|
|
52
|
+
sub.unsubscribe();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### Send a Event/Notification/Data
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const res = await client.notify({
|
|
59
|
+
recipient: "RECIPIENT_UUID",
|
|
60
|
+
payload: { title: "Hello", message: "Welcome to Arifa!" }, // Can be any JSON data
|
|
61
|
+
});
|
|
62
|
+
console.log(res);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`recipient` → UUID of the user
|
|
66
|
+
`payload`→ Any JSON object
|
|
67
|
+
`client` → Optional, "web" or "mobile"
|
|
68
|
+
|
|
69
|
+
**Note**: The notify method returns a result object with the notification status.
|
|
70
|
+
Example responses:
|
|
71
|
+
|
|
72
|
+
- "sent" → Notification successfully sent to a connected user
|
|
73
|
+
|
|
74
|
+
- "User offline, notification saved" → User is offline; the message was stored for later delivery
|
|
75
|
+
|
|
76
|
+
- "Failed to send/save notification" → There was an error sending or saving the notification
|
|
77
|
+
|
|
78
|
+
#### Disconnect
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
client.disconnect();
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### Check Connection Status
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
if (client.connected()) {
|
|
88
|
+
console.log("WebSocket is connected");
|
|
89
|
+
} else {
|
|
90
|
+
console.log("WebSocket is disconnected");
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## ## ArifaClient Usage Example
|
|
95
|
+
|
|
96
|
+
```jsx
|
|
97
|
+
import { ArifaClient } from "arifa-client";
|
|
98
|
+
|
|
99
|
+
// Initialize client
|
|
100
|
+
const client = new ArifaClient({
|
|
101
|
+
apiKey: "arifa_test:YOUR_API_KEY",
|
|
102
|
+
client: "web", // "web" or "mobile"
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Listen to connection state
|
|
106
|
+
client.onConnectionChange((state) => {
|
|
107
|
+
console.log("Connection state:", state); // "connected" or "disconnected"
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Subscribe to a recipient (UUID required)
|
|
111
|
+
const sub = client.subscribe("RECIPIENT_UUID");
|
|
112
|
+
|
|
113
|
+
// Listen for incoming events
|
|
114
|
+
sub.listen((event) => {
|
|
115
|
+
console.log("Received event:", event);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Example: Send a notification/event
|
|
119
|
+
const sendNotification = async () => {
|
|
120
|
+
const res = await client.notify({
|
|
121
|
+
recipient: "RECIPIENT_UUID",
|
|
122
|
+
payload: { title: "Hello", message: "Welcome to Arifa!" }, // Any JSON data
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
console.log(res);
|
|
126
|
+
/*
|
|
127
|
+
Possible responses:
|
|
128
|
+
"sent" → Notification successfully sent to a connected user
|
|
129
|
+
"User offline, notification saved" → User offline; message stored for later delivery
|
|
130
|
+
"Failed to send/save notification" → Error sending or saving notification
|
|
131
|
+
*/
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
sendNotification();
|
|
135
|
+
|
|
136
|
+
// Unsubscribe when done
|
|
137
|
+
// sub.unsubscribe();
|
|
138
|
+
|
|
139
|
+
// Disconnect client when done
|
|
140
|
+
// client.disconnect();
|
|
141
|
+
|
|
142
|
+
// Check connection status
|
|
143
|
+
if (client.connected()) {
|
|
144
|
+
console.log("WebSocket is connected");
|
|
145
|
+
} else {
|
|
146
|
+
console.log("WebSocket is disconnected");
|
|
147
|
+
}
|
|
148
|
+
```
|