itty-sockets 0.9.2 → 0.9.3
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 +34 -23
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -33,47 +33,58 @@ No accounts, no API keys, nothing to deploy. Just connect and start sending.
|
|
|
33
33
|
<br />
|
|
34
34
|
|
|
35
35
|
## Features
|
|
36
|
-
- **Zero
|
|
37
|
-
- **
|
|
38
|
-
- **
|
|
39
|
-
- **Send Anything** - Strings, objects, arrays — anything JSON-serializable.
|
|
36
|
+
- **Zero Config/Cost** - No accounts, no API keys, no server. Pick a channel name and you're live.
|
|
37
|
+
- **Private** - No logging, no tracking, no storage. Messages are relayed and forgotten.
|
|
38
|
+
- **Flexible** - Send any JSON-serializable data/shape.
|
|
40
39
|
- **Access Control** - [Reserve a namespace](https://ittysockets.com/reservations) to control who can join or send on your channels.
|
|
41
40
|
- **Use Anywhere** - No vendor lock. This client works with *any* WebSocket server. Want to host your own? No problem.
|
|
42
41
|
- **Tiny Client** - Only 466 bytes gzipped.
|
|
43
42
|
|
|
44
43
|
<br />
|
|
45
44
|
|
|
46
|
-
##
|
|
45
|
+
## Chat Example
|
|
47
46
|
```ts
|
|
48
47
|
import { connect } from 'itty-sockets' // ~466 bytes
|
|
49
48
|
|
|
50
|
-
connect
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
.send(
|
|
49
|
+
// connect to a room
|
|
50
|
+
const bob = connect('my-secret-chat-room', { as: 'Bob' })
|
|
51
|
+
|
|
52
|
+
bob
|
|
53
|
+
.send('hey Alice!')
|
|
54
|
+
.send([1, 2, 3])
|
|
55
|
+
.send({ foo: 'bar' })
|
|
55
56
|
```
|
|
56
57
|
|
|
57
|
-
|
|
58
|
+
meanwhile, elsewhere...
|
|
58
59
|
|
|
59
|
-
## Chat Example
|
|
60
60
|
```ts
|
|
61
|
-
|
|
61
|
+
// connect to the same room as above, and listen for messages
|
|
62
|
+
const alice = connect('my-secret-chat-room', { as: 'Alice' })
|
|
63
|
+
.on('message', ({ message, alias }) =>
|
|
64
|
+
console.log(`${alias}: ${message}`)
|
|
65
|
+
)
|
|
62
66
|
|
|
63
|
-
// two users, same channel
|
|
64
|
-
const alice = connect('chat-room', { as: 'Alice' })
|
|
65
|
-
const bob = connect('chat-room', { as: 'Bob' })
|
|
66
|
-
|
|
67
|
-
alice.on('message', ({ message, alias }) =>
|
|
68
|
-
console.log(`${alias}: ${message}`)
|
|
69
|
-
)
|
|
70
|
-
|
|
71
|
-
bob.send('hey Alice!')
|
|
72
67
|
// → "Bob: hey Alice!"
|
|
68
|
+
// → "Bob: [1, 2, 3]"
|
|
69
|
+
// → "Bob: { foo: 'bar' }"
|
|
73
70
|
```
|
|
74
71
|
|
|
75
72
|
<br />
|
|
76
73
|
|
|
74
|
+
## Installation
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
npm install itty-sockets
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Or just paste the following snippet (loses TypeScript support):
|
|
81
|
+
<!-- BEGIN SNIPPET -->
|
|
82
|
+
```ts
|
|
83
|
+
let connect=(e,s={})=>{let a,t,n=[],p={},o=()=>(a||(a=new WebSocket((/^wss?:/.test(e)?e:"wss://itty.ws/c/"+e)+"?"+new URLSearchParams(s)),a.onmessage=(e,s=JSON.parse(e.data),a=s?.message,t={...null==a?.[0]&&a,...s})=>[t.type,s.type?0:"message","*"].map(e=>p[e]?.map(e=>e(t))),a.onopen=()=>(n.splice(0).map(e=>a.send(e)),p.open?.map(e=>e(t)),t&&a?.close()),a.onclose=()=>(t=a=null,p.close?.map(e=>e(t)))),l),l={open:o,send:(e,s)=>(e=(s?`${s}`:"")+JSON.stringify(e),1&a?.readyState?a.send(e):n.push(e),o()),on:(e,s)=>((p[e?.[0]?e:"*"]??=[]).push(e?.[0]?s:a=>e?.(a)&&s(a)),o()),remove:(e,s)=>(p[e]=p[e]?.filter(e=>e!=s),l),close:()=>(1&a?.readyState?a.close():t=1,l),push:(e,s)=>(t=1,l.send(e,s))};return l};
|
|
84
|
+
```
|
|
85
|
+
<!-- END SNIPPET -->
|
|
86
|
+
|
|
87
|
+
|
|
77
88
|
## API at a Glance
|
|
78
89
|
|
|
79
90
|
| Method | Description |
|
|
@@ -82,7 +93,7 @@ bob.send('hey Alice!')
|
|
|
82
93
|
| `.on(type, listener)` | Listen for events (`'message'`, `'join'`, `'leave'`, `'open'`, `'close'`, `'error'`, custom types, or `'*'`) |
|
|
83
94
|
| `.on(filterFn, listener)` | Listen with a custom filter function |
|
|
84
95
|
| `.send(message, uid?)` | Send a message (optionally to a specific user) |
|
|
85
|
-
| `.push(message, uid?)` | Send a message and disconnect |
|
|
96
|
+
| `.push(message, uid?)` | Send a message and disconnect afterwards |
|
|
86
97
|
| `.open()` | (Re)connect — safe to call anytime, listeners are preserved |
|
|
87
98
|
| `.close()` | Disconnect |
|
|
88
99
|
| `.remove(type, listener)` | Remove a listener |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "itty-sockets",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.3",
|
|
4
4
|
"description": "WebSockets : simplified and minified.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"keywords": [
|
|
22
22
|
"websockets",
|
|
23
23
|
"realtime",
|
|
24
|
-
"client"
|
|
24
|
+
"client",
|
|
25
|
+
"zero-config"
|
|
25
26
|
],
|
|
26
27
|
"repository": {
|
|
27
28
|
"type": "git",
|