Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Latest commit

 

History

History
66 lines (52 loc) · 1.91 KB

README.md

File metadata and controls

66 lines (52 loc) · 1.91 KB

Native SSE (EventSource) Plugin for Capacitor

Because of the way Capacitor plugins are designed, it does not exactly follow the format/signature of the "real" browser EventSource API, but it works similarly.

Installation

npm install capacitor-eventsource
npx cap sync

On iOS or for pure-web use, no further steps are needed.

Android Installation

On Android, update your main activity to pull in the EventSource plugin:

import com.raccoonfink.plugins.eventsource.EventSource;

public class MainActivity extends BridgeActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      // Ex: add(TotallyAwesomePlugin.class);
      add(EventSource.class);
    }});
  }
}

Usage

EventSource.configure({
  url: 'https://my-sse-url/',
  reconnectTime: 1000,
  maxReconnectTime: 60000,
  idleTimeout: 30000,
});
EventSource.addListener('open', (openResult) => {
  console.debug('Connection opened.');
});
EventSource.addListener('message', (messageResult) => {
  console.info('I got a message: ' + messageResult.message);
});
EventSource.addListener('error', (errorResult) => {
  console.error('Uh-oh: ' + errorResult.error);
});
EventSource.addListener('readyStateChanged', (readyStateChangedResult) => {
  console.debug('Ready state changed: ' + readyStateChangedResult.state);
});

Parameters

note: all times are in milliseconds

  • url: required the URL to connect to
  • reconnectTime: time to reconnect after the first failure (iOS/Android)
  • maxReconnectTime: maximum amount of time to wait until attempting a reconnect (iOS/Android)
  • idleTimeout: maximum amount of time between receiving any data before considering the connection to have timed out (iOS/Android)