Simple.
Building realtime features is hard. I wanted to make it easy (mostly for myself).
Like... really easy.
No accounts needed. Just start sending.
import { connect } from 'itty-sockets' // ~400 bytes
Don't want to import files? Want to use it in a browser? Just paste the snippet below and start using it immediately. You'll lose TypeScript support, but this is the entire client code! I told you it was tiny.
let connect=(e,s={})=>{let t,n=[],o=[],a=0,r=()=>{t||(t=new WebSocket(`wss://ittysockets.io/r/${e??""}?${new URLSearchParams(s)}`),t.onopen=()=>{for(;n.length;)t?.send(n.shift());a&&t?.close()},t.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of o)e({...s,date:new Date(s.date)})},t.onclose=()=>(a=0,t=null))};return new Proxy(r,{get:(e,s,l)=>({ws:t,send:(e,s)=>(e=JSON.stringify(e),e=s?`@@${s}@@${e}`:e,1==t?.readyState?t.send(e)??l:(n.push(e),r()??l)),push:(e,s)=>(a=1,l.send(e,s)),listen:(e,s)=>(o.push((t=>(!s||s(t))&&e(t))),r()??l),close:()=>(1==t?.readyState?t.close():a=1,l)}[s])})};
No stringifying, parsing, or extra steps required.
const channel = connect('my-cool-channel', { echo: true })
// listen for messages
.listen(e => console.log('received:', e.message))
// send messages
.send('hello world')
.send([1,2,3])
// keep sending...
channel.send({ foo: 'bar' })
// received: hello world
// received: [1, 2, 3]
// received: { foo: 'bar' }