pingerchips-js 2.1.0 → 2.1.1
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 +57 -46
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pingerchips-js
|
|
2
2
|
|
|
3
3
|
Real-time WebSocket client for Pingerchips.
|
|
4
4
|
|
|
@@ -10,17 +10,29 @@ npm install pingerchips-js
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
-
### Basic
|
|
13
|
+
### Basic connection
|
|
14
14
|
|
|
15
15
|
```javascript
|
|
16
16
|
import Pingerchips from 'pingerchips-js';
|
|
17
17
|
|
|
18
|
+
// Connects to queue.pingerchips.com in production by default
|
|
19
|
+
const client = new Pingerchips('your_app_key');
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Pass `debug: "false"` to force the production endpoint, or override with `endpoint`:
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
const client = new Pingerchips('your_app_key', {
|
|
26
|
+
debug: "false" // uses wss://queue.pingerchips.com/socket
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// or explicit:
|
|
18
30
|
const client = new Pingerchips('your_app_key', {
|
|
19
|
-
endpoint: 'wss://
|
|
31
|
+
endpoint: 'wss://your-own-deployment.com/socket'
|
|
20
32
|
});
|
|
21
33
|
```
|
|
22
34
|
|
|
23
|
-
###
|
|
35
|
+
### Public channels
|
|
24
36
|
|
|
25
37
|
```javascript
|
|
26
38
|
const channel = await client.subscribe('lobby');
|
|
@@ -29,77 +41,76 @@ channel.bind('message', (data) => {
|
|
|
29
41
|
console.log('Received:', data);
|
|
30
42
|
});
|
|
31
43
|
|
|
44
|
+
// Send client event
|
|
32
45
|
channel.trigger('message', { text: 'Hello!' });
|
|
33
46
|
```
|
|
34
47
|
|
|
35
|
-
###
|
|
48
|
+
### Private channels
|
|
36
49
|
|
|
37
|
-
|
|
50
|
+
Requires an `authEndpoint` on your server (see `pingerchips-js-server`):
|
|
38
51
|
|
|
39
52
|
```javascript
|
|
40
53
|
const client = new Pingerchips('your_app_key', {
|
|
41
|
-
endpoint: 'wss://pinger-processor.pingerchips.com/socket',
|
|
42
54
|
authEndpoint: 'https://your-server.com/auth',
|
|
43
|
-
authInfo: {
|
|
44
|
-
userId: '123',
|
|
45
|
-
token: 'user-session-token'
|
|
46
|
-
}
|
|
55
|
+
authInfo: { token: 'user-session-token' } // forwarded to your auth endpoint
|
|
47
56
|
});
|
|
48
57
|
|
|
49
|
-
const
|
|
58
|
+
const channel = await client.subscribe('private-chat');
|
|
50
59
|
```
|
|
51
60
|
|
|
52
|
-
###
|
|
61
|
+
### Presence channels
|
|
53
62
|
|
|
54
63
|
```javascript
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
console.log('User joined:', data.user_info);
|
|
64
|
+
const client = new Pingerchips('your_app_key', {
|
|
65
|
+
authEndpoint: 'https://your-server.com/auth',
|
|
66
|
+
authInfo: { token: 'user-session-token' }
|
|
59
67
|
});
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
## Configuration Options
|
|
63
68
|
|
|
64
|
-
|
|
65
|
-
|--------|------|-------------|
|
|
66
|
-
| `endpoint` | string | WebSocket endpoint URL |
|
|
67
|
-
| `authEndpoint` | string | Your server's auth endpoint for private/presence channels |
|
|
68
|
-
| `authInfo` | object | User authentication info passed to your auth endpoint |
|
|
69
|
-
| `authHeaders` | object | Additional headers for auth requests |
|
|
69
|
+
const channel = await client.subscribe('presence-lobby');
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
## Channel Methods
|
|
76
|
-
|
|
77
|
-
### `bind(event, callback)`
|
|
78
|
-
|
|
79
|
-
Listen for events on a channel.
|
|
80
|
-
|
|
81
|
-
```javascript
|
|
82
|
-
channel.bind('message', (data) => {
|
|
83
|
-
console.log(data);
|
|
71
|
+
channel.bind('presence_diff', ({ joins, leaves }) => {
|
|
72
|
+
console.log('Joined:', joins);
|
|
73
|
+
console.log('Left:', leaves);
|
|
84
74
|
});
|
|
85
75
|
```
|
|
86
76
|
|
|
87
|
-
###
|
|
77
|
+
### Reconnection
|
|
78
|
+
|
|
79
|
+
The client automatically reconnects on disconnect. All previously subscribed channels are re-joined transparently — no extra handling needed.
|
|
88
80
|
|
|
89
|
-
|
|
81
|
+
### Socket ID
|
|
90
82
|
|
|
91
83
|
```javascript
|
|
92
|
-
|
|
84
|
+
// Available after first channel join
|
|
85
|
+
const socketId = client.getSocketId();
|
|
93
86
|
```
|
|
94
87
|
|
|
95
|
-
###
|
|
96
|
-
|
|
97
|
-
Stop listening for an event.
|
|
88
|
+
### Unsubscribe
|
|
98
89
|
|
|
99
90
|
```javascript
|
|
100
|
-
|
|
91
|
+
client.unsubscribe('lobby');
|
|
101
92
|
```
|
|
102
93
|
|
|
94
|
+
## Configuration
|
|
95
|
+
|
|
96
|
+
| Option | Type | Default | Description |
|
|
97
|
+
|---|---|---|---|
|
|
98
|
+
| `endpoint` | string | auto | WebSocket endpoint. Defaults to `wss://queue.pingerchips.com/socket` (prod) or `ws://localhost:4000/socket` (dev) |
|
|
99
|
+
| `debug` | string | — | Set to `"false"` to use production endpoint without providing `endpoint` explicitly |
|
|
100
|
+
| `authEndpoint` | string | — | Your server's auth URL for private/presence channels |
|
|
101
|
+
| `authInfo` | object | `{}` | Forwarded to your auth endpoint in the request body |
|
|
102
|
+
| `authHeaders` | object | — | Extra headers added to auth requests |
|
|
103
|
+
| `params` | object | — | Extra params sent with the WebSocket connection |
|
|
104
|
+
|
|
105
|
+
## Channel API
|
|
106
|
+
|
|
107
|
+
| Method | Description |
|
|
108
|
+
|---|---|
|
|
109
|
+
| `bind(event, callback)` | Listen for an event |
|
|
110
|
+
| `unbind(event)` | Stop listening for an event |
|
|
111
|
+
| `trigger(event, data)` | Send a client event |
|
|
112
|
+
| `leave()` | Leave the channel |
|
|
113
|
+
|
|
103
114
|
## License
|
|
104
115
|
|
|
105
116
|
MIT
|