JSPM

nanosocket

1.1.0
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 9015
    • Score
      100M100P100Q123798F
    • License pemrouz.mit-license.org

    tiny modern websocket client

    Package Exports

    • nanosocket
    • nanosocket/fake

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nanosocket) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    Nanosocket

    • Tiny: ~20 lines, ~1 kb bundled
    • Handles reconnection with exponential backoff
    • Receive responses via callback/promise/stream (checkout emitterify)

    example.html:

    const socket = nanosocket('wss://echo.websocket.org')
    // to connect to the same origin:
    // const socket = nanosocket()
    
    // log stream of incoming messages
    socket.on('recv').map(d => console.log("recv", d))
    
    // log all connection events
    socket.on('connected').map(d => console.log("connected", d))
    
    // log all disconnection events
    socket.on('disconnected').map(d => console.log("disconnected", d))
    
    // send a message
    // note that this returns a promsie for when the message is actually sent
    // since you can send when disconnected, and it will send after reconnecting etc
    socket.send('boo').then(d => console.log("sent", d))