itty-sockets 0.3.5 → 0.5.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.
- package/README.md +12 -7
- package/connect.js +1 -1
- package/connect.mjs +1 -1
- package/index.js +1 -1
- package/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ Tiny realtime messaging client in under 500 bytes. **No backend needed.**
|
|
|
22
22
|
|
|
23
23
|
Itty Sockets simplifies sending/receiving realtime data.
|
|
24
24
|
|
|
25
|
-
By pairing an ultra-tiny client (this) with the public **[ittysockets.io](https://ittysockets.io)** backend, you
|
|
25
|
+
By pairing an ultra-tiny client (this) with the public **[ittysockets.io](https://ittysockets.io)** backend, you
|
|
26
26
|
can focus on sending/receiving messages, instead of building a transport layer.
|
|
27
27
|
|
|
28
28
|
The idea is simple:
|
|
@@ -50,7 +50,7 @@ foo
|
|
|
50
50
|
|
|
51
51
|
### Important Considerations
|
|
52
52
|
|
|
53
|
-
1. **There is no history/replay/storage.** It's a live stream only.
|
|
53
|
+
1. **There is no history/replay/storage.** It's a live stream only.
|
|
54
54
|
2. **We don't authenticate.** [ittysockets.io](https://ittysockets.io) leverages security through obfuscation (a near-infinite number of channel names). Choose a more unique channel for more privacy. Need more? Consider encrypting/decrypting your payloads before transmission (this is easy).
|
|
55
55
|
3. **There are no guarantees of delivery.** While [ittysockets.io](https://ittysockets.io) is *extremely* stable, it's a free public service that is provided without any guarantees of delivery or uptime. Manage risk accordingly.
|
|
56
56
|
|
|
@@ -66,15 +66,17 @@ import { connect } from 'itty-sockets'
|
|
|
66
66
|
...or simply paste this into your environment/console:
|
|
67
67
|
<!-- BEGIN SNIPPET -->
|
|
68
68
|
```ts
|
|
69
|
-
let connect=(e,
|
|
69
|
+
let connect=(e,s={})=>{let o,t=[],n=0,a={},r=()=>(o||(o=new WebSocket(/^(wss?):/.test(e)?e:`wss://ittysockets.io/c/${e}?`+new URLSearchParams(s)),o.onopen=()=>{for(;t.length;)o?.send(t.shift());for(let e of a.open??[])e();n&&o?.close()},o.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of a[s.type??"message"]??[])e({...s,date:new Date(s.date)})},o.onclose=()=>{n=0,o=null;for(let e of a.close??[])e()}),l);const l=new Proxy(r,{get:(e,s)=>({open:r,close:()=>(1==o?.readyState?o.close():n=1,l),send:(e,s)=>(e=JSON.stringify(e),e=s?`@@${s}@@${e}`:e,1==o?.readyState?(o.send(e),l):(t.push(e),r())),push:(e,s)=>(n=1,l.send(e,s)),on:(e,s)=>((a[e]??=[]).push(s),r()),remove:(e,s,o=a[e],t=o?.indexOf(s)??-1)=>(~t&&o?.splice(t,1),r())}[s])});return l};
|
|
70
70
|
```
|
|
71
71
|
<!-- END SNIPPET -->
|
|
72
72
|
|
|
73
73
|
<br />
|
|
74
74
|
|
|
75
|
-
### 2.
|
|
75
|
+
### 2. Connect to a Channel (or external server)
|
|
76
76
|
To start, simply connect to a channel based on a unique name (this can be anything).
|
|
77
77
|
|
|
78
|
+
> **NOTE:** Pass a valid `ws://` or `wss://` URL as the channel identifier to bypass the public [ittysockets.io](https://ittysockets.io) service and use your own.
|
|
79
|
+
|
|
78
80
|
```ts
|
|
79
81
|
import { connect } from 'itty-sockets'
|
|
80
82
|
|
|
@@ -87,15 +89,18 @@ const channel = connect('my-channels/my-super-secret-channel', {
|
|
|
87
89
|
announce: true,
|
|
88
90
|
echo: true
|
|
89
91
|
})
|
|
92
|
+
|
|
93
|
+
// an external server
|
|
94
|
+
const channel = connect('wss://somewhere.else/entirely')
|
|
90
95
|
```
|
|
91
96
|
|
|
92
97
|
#### Connection Options
|
|
93
98
|
|
|
94
|
-
| option | default value | description |
|
|
99
|
+
| option | default value | description |
|
|
95
100
|
| --- | --- | --- |
|
|
96
101
|
| `{ alias: 'any-string' }` | `undefined` | An optional display name to be included in your messages. |
|
|
97
|
-
| `{ as: 'any-string' }` | `undefined` | An optional display name to be included in your message (same as alias). |
|
|
98
|
-
| `{ announce: true }` | `false` | Shares your uid/alias when joining/leaving. |
|
|
102
|
+
| `{ as: 'any-string' }` | `undefined` | An optional display name to be included in your message (same as alias). |
|
|
103
|
+
| `{ announce: true }` | `false` | Shares your uid/alias when joining/leaving. |
|
|
99
104
|
| `{ echo: true }` | `false` | Echos messages back to original sender (good for testing). |
|
|
100
105
|
|
|
101
106
|
<br />
|
package/connect.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";exports.connect=(e,s={})=>{let o
|
|
1
|
+
"use strict";exports.connect=(e,s={})=>{let t,o=[],n=0,r={},a=()=>(t||(t=new WebSocket(/^(wss?):/.test(e)?e:`wss://ittysockets.io/c/${e}?`+new URLSearchParams(s)),t.onopen=()=>{for(;o.length;)t?.send(o.shift());for(let e of r.open??[])e();n&&t?.close()},t.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of r[s.type??"message"]??[])e({...s,date:new Date(s.date)})},t.onclose=()=>{n=0,t=null;for(let e of r.close??[])e()}),c);const c=new Proxy(a,{get:(e,s)=>({open:a,close:()=>(1==t?.readyState?t.close():n=1,c),send:(e,s)=>(e=JSON.stringify(e),e=s?`@@${s}@@${e}`:e,1==t?.readyState?(t.send(e),c):(o.push(e),a())),push:(e,s)=>(n=1,c.send(e,s)),on:(e,s)=>((r[e]??=[]).push(s),a()),remove:(e,s,t=r[e],o=t?.indexOf(s)??-1)=>(~o&&t?.splice(o,1),a())}[s])});return c};
|
package/connect.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=(e,
|
|
1
|
+
const e=(e,s={})=>{let o,t=[],n=0,a={},r=()=>(o||(o=new WebSocket(/^(wss?):/.test(e)?e:`wss://ittysockets.io/c/${e}?`+new URLSearchParams(s)),o.onopen=()=>{for(;t.length;)o?.send(t.shift());for(let e of a.open??[])e();n&&o?.close()},o.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of a[s.type??"message"]??[])e({...s,date:new Date(s.date)})},o.onclose=()=>{n=0,o=null;for(let e of a.close??[])e()}),l);const l=new Proxy(r,{get:(e,s)=>({open:r,close:()=>(1==o?.readyState?o.close():n=1,l),send:(e,s)=>(e=JSON.stringify(e),e=s?`@@${s}@@${e}`:e,1==o?.readyState?(o.send(e),l):(t.push(e),r())),push:(e,s)=>(n=1,l.send(e,s)),on:(e,s)=>((a[e]??=[]).push(s),r()),remove:(e,s,o=a[e],t=o?.indexOf(s)??-1)=>(~t&&o?.splice(t,1),r())}[s])});return l};export{e as connect};
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";exports.connect=(e,s={})=>{let o
|
|
1
|
+
"use strict";exports.connect=(e,s={})=>{let t,o=[],n=0,r={},a=()=>(t||(t=new WebSocket(/^(wss?):/.test(e)?e:`wss://ittysockets.io/c/${e}?`+new URLSearchParams(s)),t.onopen=()=>{for(;o.length;)t?.send(o.shift());for(let e of r.open??[])e();n&&t?.close()},t.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of r[s.type??"message"]??[])e({...s,date:new Date(s.date)})},t.onclose=()=>{n=0,t=null;for(let e of r.close??[])e()}),c);const c=new Proxy(a,{get:(e,s)=>({open:a,close:()=>(1==t?.readyState?t.close():n=1,c),send:(e,s)=>(e=JSON.stringify(e),e=s?`@@${s}@@${e}`:e,1==t?.readyState?(t.send(e),c):(o.push(e),a())),push:(e,s)=>(n=1,c.send(e,s)),on:(e,s)=>((r[e]??=[]).push(s),a()),remove:(e,s,t=r[e],o=t?.indexOf(s)??-1)=>(~o&&t?.splice(o,1),a())}[s])});return c};
|
package/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=(e,
|
|
1
|
+
const e=(e,s={})=>{let o,t=[],n=0,a={},r=()=>(o||(o=new WebSocket(/^(wss?):/.test(e)?e:`wss://ittysockets.io/c/${e}?`+new URLSearchParams(s)),o.onopen=()=>{for(;t.length;)o?.send(t.shift());for(let e of a.open??[])e();n&&o?.close()},o.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of a[s.type??"message"]??[])e({...s,date:new Date(s.date)})},o.onclose=()=>{n=0,o=null;for(let e of a.close??[])e()}),l);const l=new Proxy(r,{get:(e,s)=>({open:r,close:()=>(1==o?.readyState?o.close():n=1,l),send:(e,s)=>(e=JSON.stringify(e),e=s?`@@${s}@@${e}`:e,1==o?.readyState?(o.send(e),l):(t.push(e),r())),push:(e,s)=>(n=1,l.send(e,s)),on:(e,s)=>((a[e]??=[]).push(s),r()),remove:(e,s,o=a[e],t=o?.indexOf(s)??-1)=>(~t&&o?.splice(t,1),r())}[s])});return l};export{e as connect};
|