Versioned envelopes and the dependency-free client

Documentation for Redweb 0.13.2. Install that exact version when following these examples.

Negotiate a finite protocol version before upgrade, then share stable envelopes and error codes between server and client.

This pattern demonstrates one API area. Application-specific names, credentials, assets, and policies may need to be supplied. Complete starter recipes include all required application files.

js
const { SocketRoute } = require('redweb')

class ProtocolRoute extends SocketRoute {
  constructor() {
    super({
      path: '/match',
      handlers: [MoveHandler],
      protocol: {
        versions: ['2', '1'],
        binary: {
          maxBytes: 64 * 1024,
          encode: (state) => codec.encode(state),
          decode: (bytes) => codec.decode(bytes),
        },
      },
    })
  }
}

// Browser client
const { ProtocolClient, ERROR_CODES } = require('redweb/client')
const socket = new WebSocket(
  'wss://game.example/match?redwebVersion=2'
)
const client = new ProtocolClient(socket, '2')

socket.addEventListener('message', (event) => {
  const message = client.parse(event)
  if (message.error?.code === ERROR_CODES.RATE_LIMITED) backOff()
})

client.send('move', { x: 4, y: 2 }, { sequence: 17 })

Notes and boundaries

  • Browsers negotiate with redwebVersion in the query.
  • requestId correlates; sequence expresses application ordering.
  • Neither field promises durability or exactly-once delivery.