pingerchips-js 2.1.0 → 3.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 (5) hide show
  1. package/README.md +57 -46
  2. package/chat.js +585 -0
  3. package/index.js +835 -43
  4. package/package.json +3 -2
  5. package/spaces.js +414 -0
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Pingerchips JavaScript SDK
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 Connection
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://pinger-processor.pingerchips.com/socket'
31
+ endpoint: 'wss://your-own-deployment.com/socket'
20
32
  });
21
33
  ```
22
34
 
23
- ### Subscribe to Public Channels
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
- ### Subscribe to Private Channels
48
+ ### Private channels
36
49
 
37
- For private channels, you need to configure an auth endpoint:
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 privateChannel = await client.subscribe('private-chat');
58
+ const channel = await client.subscribe('private-chat');
50
59
  ```
51
60
 
52
- ### Subscribe to Presence Channels
61
+ ### Presence channels
53
62
 
54
63
  ```javascript
55
- const presenceChannel = await client.subscribe('presence-lobby');
56
-
57
- presenceChannel.bind('user-joined', (data) => {
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
- | Option | Type | Description |
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
- ## Authentication
72
-
73
- For private and presence channels, you must implement an auth endpoint on your server. See [pingerchips-js-server](https://www.npmjs.com/package/pingerchips-js-server) for server-side implementation.
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
- ### `trigger(event, data)`
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
- Send an event to a channel.
81
+ ### Socket ID
90
82
 
91
83
  ```javascript
92
- channel.trigger('message', { text: 'Hello!' });
84
+ // Available after first channel join
85
+ const socketId = client.getSocketId();
93
86
  ```
94
87
 
95
- ### `unbind(event)`
96
-
97
- Stop listening for an event.
88
+ ### Unsubscribe
98
89
 
99
90
  ```javascript
100
- channel.unbind('message');
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