# binaryminds/react-native-sse architecture

> Architecture diagram of the GitHub repository binaryminds/react-native-sse, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/binaryminds/react-native-sse
- Repository: https://github.com/binaryminds/react-native-sse

## Overview

##### Main Components
- Library API / Interface: Represented by the index.js and index.d.ts files, which serve as the public entry point to the library. This is where users import and construct their EventSource instances.
- Core EventSource Module: Located in src/EventSource.js. This module contains the logic to create a connection (via XMLHttpRequest), handle event listeners, and manage options such as headers, HTTP methods, timeouts, etc.
- Event Handling & Listeners: Represent the internal observer mechanism that listens to events (“open”, “message”, “error”, “close”, and custom events) and dispatches these to user-supplied callbacks.

##### Detail the Relationships and Interactions
- Flow from the consuming application (the client code or React Native app) importing the library (via index.js/index.d.ts).
- From the public API, illustrate how a new EventSource object is instantiated with a URL and options.
- Demonstrate the internal connection lifecycle: the EventSource module uses XMLHttpRequest under the hood to establish and maintain an SSE connection.
- Registration of event listeners on the EventSource instance and how events are handled and forwarded to client code.
- How configuration options are passed through and applied to the connection behavior.

##### Architectural Patterns and Principles
- Observer/Listener Pattern: Emphasize the event-driven nature where subscribers register listeners for different event types.
- Modular Design: The separation of the public interface (index.js / index.d.ts) from the implementation inside the src directory.
- Abstraction over Native Networking: The library abstracts away the complexities of using XMLHttpRequest to provide a simpler API similar to the native EventSource interface.

##### Incorporate Relevant Technologies and Frameworks
- React Native: Since this is aimed at React Native, note that the underlying environment is a mobile JavaScript runtime.
- XMLHttpRequest: Make clear that this API is used for establishing and handling SSE connections.
- TypeScript: Use of index.d.ts shows that type definitions are available, enhancing development experience.
- Any usage of additional polyfills or middleware (e.g. “react-native-url-polyfill”) can be noted as external dependencies for URL handling.

##### Essential Elements
- Clear labels for all components (e.g., “EventSource API”, “XHR Connection”, “Event Listener Module”, “Configuration Handler”).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Consumer"
        RN["React Native App (Client Code)"]:::external
    end

    subgraph "Public Interface"
        API_JS["index.js"]:::public
        API_DTS["index.d.ts"]:::public
    end

    RN -->|"imports"| API_JS
    RN -->|"imports"| API_DTS

    API_JS -->|"creates"| CORE
    API_DTS -->|"creates"| CORE

    CORE["Core EventSource Logic (src/EventSource.js)"]:::core

    subgraph "Internal Modules"
        CONFIG["Configuration Options"]:::internal
        XHR["XHR Connection (XMLHttpRequest)"]:::internal
        OBSERVER["Event Listener Module"]:::internal
    end

    CORE -->|"appliesConfig"| CONFIG
    CORE -->|"establishesConn"| XHR
    CORE -->|"registersListeners"| OBSERVER
    XHR -->|"emitsEvents"| OBSERVER
    OBSERVER -->|"dispatchesEvents"| API_JS
    OBSERVER -->|"dispatchesEvents"| API_DTS
    API_JS -->|"returnsCallbacks"| RN
    API_DTS -->|"returnsCallbacks"| RN

    click API_JS "https://github.com/binaryminds/react-native-sse/blob/master/index.js"
    click API_DTS "https://github.com/binaryminds/react-native-sse/blob/master/index.d.ts"
    click CORE "https://github.com/binaryminds/react-native-sse/blob/master/src/EventSource.js"

    classDef external fill:#cce5ff,stroke:#0056b3,stroke-width:2px;
    classDef public fill:#ffe6cc,stroke:#cc7a00,stroke-width:2px;
    classDef core fill:#f0e6ff,stroke:#800080,stroke-width:2px;
    classDef internal fill:#e6ffe6,stroke:#009933,stroke-width:2px;
```
