itty-sockets 0.9.1 → 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.
Files changed (2) hide show
  1. package/README.md +36 -27
  2. package/package.json +3 -2
package/README.md CHANGED
@@ -14,7 +14,7 @@
14
14
  [![Issues](https://img.shields.io/github/issues/kwhitley/itty-sockets?style=flat-square)](https://github.com/kwhitley/itty-sockets/issues)
15
15
  [![Discord](https://img.shields.io/discord/832353585802903572?label=Discord&logo=Discord&style=flat-square&logoColor=fff)](https://discord.gg/53vyrZAu9u)
16
16
 
17
- ### [Full Documentation](https://itty.ws/docs)  |   [Discord](https://discord.gg/53vyrZAu9u)
17
+ ### [Full Documentation](https://ittysockets.com/docs)  |   [Discord](https://discord.gg/53vyrZAu9u)
18
18
 
19
19
  ---
20
20
 
@@ -26,56 +26,65 @@ No accounts, no API keys, nothing to deploy. Just connect and start sending.
26
26
 
27
27
  <br />
28
28
 
29
- > After working in realtime for quite a few years, I wanted something absolutely frictionless for prototyping. Spinning up socket servers or authenticating to services like Pusher/Ably involves overhead every time... so I built a service for myself (and everyone else). Then I designed this super-tiny WebSocket client that made even *that* side really easy to work with.
29
+ > After building realtime apps for years, I wanted something absolutely frictionless for prototyping. Spinning up socket servers or authenticating to services like Pusher/Ably involves overhead every time... so I built a service for myself (and everyone else). Then I designed this super-tiny WebSocket client that made even *that* side really easy to work with.
30
30
  >
31
31
  > Welcome to `itty-sockets`!
32
- >
33
- > ~ Kevin W
34
32
 
35
33
  <br />
36
34
 
37
35
  ## Features
38
- - **Zero Configuration** - No accounts, no API keys, no server. Pick a channel name and you're live.
39
- - **Zero Cost** - No tiers. No credit card. Built for the community.
40
- - **Private by Default** - No logging, no tracking, no storage. Messages are relayed and forgotten.
41
- - **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.
42
39
  - **Access Control** - [Reserve a namespace](https://ittysockets.com/reservations) to control who can join or send on your channels.
43
40
  - **Use Anywhere** - No vendor lock. This client works with *any* WebSocket server. Want to host your own? No problem.
44
41
  - **Tiny Client** - Only 466 bytes gzipped.
45
42
 
46
43
  <br />
47
44
 
48
- ## Quick Start
45
+ ## Chat Example
49
46
  ```ts
50
47
  import { connect } from 'itty-sockets' // ~466 bytes
51
48
 
52
- connect('my-channel')
53
- .on('message', ({ message }) => console.log(message))
54
- .send('hello world') // strings
55
- .send([1, 2, 3]) // arrays
56
- .send({ foo: 'bar' }) // objects
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' })
57
56
  ```
58
57
 
59
- <br />
58
+ meanwhile, elsewhere...
60
59
 
61
- ## Chat Example
62
60
  ```ts
63
- import { connect } from 'itty-sockets'
64
-
65
- // two users, same channel
66
- const alice = connect('chat-room', { as: 'Alice' })
67
- const bob = connect('chat-room', { as: 'Bob' })
68
-
69
- alice.on('message', ({ message, alias }) =>
70
- console.log(`${alias}: ${message}`)
71
- )
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
+ )
72
66
 
73
- bob.send('hey Alice!')
74
67
  // → "Bob: hey Alice!"
68
+ // → "Bob: [1, 2, 3]"
69
+ // → "Bob: { foo: 'bar' }"
75
70
  ```
76
71
 
77
72
  <br />
78
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
+
79
88
  ## API at a Glance
80
89
 
81
90
  | Method | Description |
@@ -84,7 +93,7 @@ bob.send('hey Alice!')
84
93
  | `.on(type, listener)` | Listen for events (`'message'`, `'join'`, `'leave'`, `'open'`, `'close'`, `'error'`, custom types, or `'*'`) |
85
94
  | `.on(filterFn, listener)` | Listen with a custom filter function |
86
95
  | `.send(message, uid?)` | Send a message (optionally to a specific user) |
87
- | `.push(message, uid?)` | Send a message and disconnect |
96
+ | `.push(message, uid?)` | Send a message and disconnect afterwards |
88
97
  | `.open()` | (Re)connect — safe to call anytime, listeners are preserved |
89
98
  | `.close()` | Disconnect |
90
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.1",
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",