Giter Site home page Giter Site logo

eventsource's Introduction

EventSource

EventSource is a Swift package that provides a simple implementation of a client for Server-Sent Events (SSE). It allows you to easily receive real-time updates from a server over a persistent HTTP connection, using a simple and efficient interface.

It also leverages Swift concurrency features to provide a more expressive and intuitive way to handle asynchronous operations.

Note Please note that this package was originally developed to be used in conjunction with another package, and as such, it may not cover all specification details. Please be aware of this limitation when evaluating whether EventSource is suitable for your specific use case.

Installation

The module name of the package is EventSource. Choose one of the instructions below to install and add the following import statement to your source code.

import EventSource

From Xcode menu: File > Swift Packages > Add Package Dependency

https://github.com/Recouse/EventSource

In your Package.swift file, first add the following to the package dependencies:

.package(url: "https://github.com/Recouse/EventSource.git"),

And then, include "EventSource" as a dependency for your target:

.target(name: "<target>", dependencies: [
    .product(name: "EventSource", package: "EventSource"),
]),

Usage

Using EventSource is easy. Simply create an instance of EventSource with the URLReqh uest of the SSE endpoint you want to connect to, and await for events:

import EventSource

...

let eventSource = EventSource(request: urlRequest)
eventSource.connect()

for await event in eventSource.events {
    switch event {
    case .open:
        print("Connection was opened.")
    case .error(let error):
        print("Received an error:", error.localizedDescription)
    case .message(let message):
        print("Received a message", message.data ?? "")
    case .closed:
        print("Connection was closed.")
    }
}

Use close() to explicilty close the connection.

Compatibility

  • macOS 10.15+
  • iOS 13.0+
  • tvOS 13.0+
  • watchOS 6.0+
  • visionOS 1.0+

Dependencies

Contributing

Contributions to are always welcomed! If you'd like to contribute, please fork this repository and submit a pull request with your changes.

License

EventSource is released under the MIT License. See LICENSE for more information.

eventsource's People

Contributors

isontheline avatar recouse avatar winddpan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.