Code Monkey home page Code Monkey logo

eventsource's Introduction

EventSource

A simple Swift event source for fun and profit

Swift Carthage

EventSource is based on the EventSource Web API to enable Server Sent Events.

If you're unfamiliar with this one-way streaming protocol - Start Here.

Under the hood, EventSource is built on top of NSURLSession and has zero third-party dependencies.

Enjoy!

Usage

An Event looks like this

struct Event {

  let readyState: EventSourceState // The EventSourceState at the time of the event's creation

  let id: String?
  let name: String?
  let data: String?
  let error: NSError?
}

You create an EventSource with an NSURL

import EventSource

let url = NSURL(string: "http://localhost:8000/stream")!
        
let eventSource = EventSource(url: url)

Opening and closing the connection

eventSource.open()
eventSource.close()

Adding standard event handlers

eventSource.onOpen { event in
  debugPrint(event)
}

eventSource.onMessage { event in
  debugPrint(event)
}

eventSource.onClose { event in
  debugPrint(event)
}

eventSource.onError { event in
  debugPrint(event)
}

Adding named event handlers

eventSource.addHandler("tweet.create") { event in
  debugPrint(event.data)
}

Example

In the Example directory, you'll find the Server and EventSourceExample directories. The Server directory contains a simple python server that sends events to any connected clients, and the EventSourceExample directory contains a simple iOS app to display recent events from that server.

Server Setup

The server uses Redis to setup pub / sub channels, and it uses Flask deployed with Gunicorn to serve events to connected clients.

Install the following packages to run the simple python server

brew install redis
pip install flask redis gevent gunicorn

Start redis and deploy the server (in two separate terminal tabs)

redis-server
gunicorn --worker-class=gevent -b 0.0.0.0:8000 app:app

Client Setup

Open the EventSourceExample Xcode project and run the app in the simulator
Tap the "Open" button in the app to open a connection to the server

Sending Events

Now you can visit http://localhost:8000/publish in your browser to start sending events


Demo

If all goes well, you should get a nice stream of events in your simulator

alt tag


Heads Up

API Decisions

EventSource deviates slightly from the Web API where it made sense for a better iOS API. For example, an Event has a name property so you can subscribe to specific, named events like tweet.create. This is in lieu of the Web API's event property of an Event (because who wants to write let event = event.event? Not me... ๐Ÿ˜ž).

Auto-Reconnect

An EventSource will automatically reconnect to the server if it enters an Error state, and based on the protocol, a server can send a retry event with an interval indicating how frequently the EventSource should retry the connection after encountering an error. Be warned: an EventSource expects this interval to be in seconds - not milliseconds as described by the Web API.


Installation

Carthage

Add the following line to your Cartfile.

github "christianbator/EventSource"

Then run

carthage update --platform iOS

eventsource's People

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.