anycable / anycable-client

AnyCable / Action Cable JavaScript client for web, Node.js & React Native
MIT License
96 stars 15 forks source link

Add info event to propagate protocol-level events #43

Closed palkan closed 3 months ago

palkan commented 3 months ago

Context

Our extended protocol supports automatic stream history retrieval but we do not provide any APIs to react on the corresponding events (more precisely, protocol messages: confirm_history and reject_history).

This PR introduces a new info event for cables and channels that can be used to signal arbitrary protocol-level events to users, including history_not_found and history_received events:


import { createCable, Channel } from '@anycable/web'

const cable = createCable({protocol: 'actioncable-v1-ext-json'});

const ch = cable.subscribeTo("ChatChannel");

ch.on("info", (evt) => {
  if (evt.type === "history_not_found") {
    // Restore state by performing an action
    ch.perform("resetState")

    // Or hard-reset the clients by reloading the page
    window.location.reload();
  }

  // Successful history retrieval is also notified
  if (evt.type === "history_received") {
    // ...
  }
});