Skip to content

kwhitley/itty-sockets

Repository files navigation


itty-sockets

GitHub Version Bundle Size Build Status Coverage Status Issues Discord


Zero-Config WebSockets.

No accounts, no API keys, nothing to deploy. Just connect and start sending.

Chat Example

import { connect } from 'itty-sockets' // ~466 bytes

// Alice joins a room and listens
connect('chat-room', { as: 'Alice' })
  .on('message', ({ alias, text, mood }) =>
    console.log(`${alias} (${mood}): ${text}`)
  )

meanwhile, elsewhere...

// Bob joins the same room and sends a message
const bob = connect('chat-room', { as: 'Bob' })

bob.send({
  text: 'hey Alice!',
  mood: 'excited',
})

// Alice's console: → "Bob (excited): hey Alice!"

Comparison

Pusher / Ably Socket.IO itty-sockets
Time to first message sign up → key → SDK → connect deploy server → SDK → connect connect('foo').send(...)
Account / API key required N/A (self-host) none
Server to run none (hosted) yes none (hosted)
Free tier limited N/A it's all free
Client size ~30–50kB ~40kB 466 bytes
Logging / data retention yes up to you none

Installation

npm install itty-sockets

Or just paste the following snippet (loses TypeScript support):

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};

API at a Glance

Method Description
connect(channel, options?) Connect to a channel (or raw wss:// URL)
.on(type, listener) Listen for events ('message', 'join', 'leave', 'open', 'close', 'error', custom types, or '*')
.on(filterFn, listener) Listen with a custom filter function
.send(message, uid?) Send a message (optionally to a specific user)
.push(message, uid?) Send a message and disconnect afterwards
.open() (Re)connect — safe to call anytime, listeners are preserved
.close() Disconnect
.remove(type, listener) Remove a listener

About

A client connector for the itty-sockets service.

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

 
 
 

Contributors

Generated from kwhitley/itty-chroma