inaka

Latest blog entries

/
Erlang and Elixir Factory Lite B.A.

A brief introduction about what was the Erlang Factory Conference in Buenos Aires for some Inaka team members

Jul 07 2017 : Euen Lopez

/
The Art of Writing a Blogpost

The Art of Writing a Blogpost

Apr 11 2017 : Matias Vera

/
SpellingCI: No more spelling mistakes in your markdown flies!

Feb 14 2017 : Felipe Ripoll

/
Fast reverse geocoding with offline-geocoder

Do you need a blazing fast reverse geocoder? Enter offline-geocoder!

Jan 18 2017 : Roberto Romero

/
Using Jayme to connect to the new MongooseIM REST services

MongooseIM has RESTful services!! Here I show how you can use them in an iOS application.

Dec 13 2016 : Sergio Abraham

/
20 Questions, or Maybe a Few More

20 Questions, or Maybe a Few More

Nov 16 2016 : Stephanie Goldner

/
The Power of Meeting People

Because conferences and meetups are not just about the technical stuff.

Nov 01 2016 : Pablo Villar

/
Finding the right partner for your app build

Sharing some light on how it is to partner with us.

Oct 27 2016 : Inaka

/
Just Play my Sound

How to easily play a sound in Android

Oct 25 2016 : Giaquinta Emiliano

/
Opening our Guidelines to the World

We're publishing our work guidelines for the world to see.

Oct 13 2016 : Brujo Benavides

/
Using NIFs: the easy way

Using niffy to simplify working with NIFs on Erlang

Oct 05 2016 : Hernan Rivas Acosta

/
Function Naming In Swift 3

How to write clear function signatures, yet expressive, while following Swift 3 API design guidelines.

Sep 16 2016 : Pablo Villar

/
Jenkins automated tests for Rails

How to automatically trigger rails tests with a Jenkins job

Sep 14 2016 : Demian Sciessere

/
Erlang REST Server Stack

A description of our usual stack for building REST servers in Erlang

Sep 06 2016 : Brujo Benavides

/
Replacing JSON when talking to Erlang

Using Erlang's External Term Format

Aug 17 2016 : Hernan Rivas Acosta

/
Gadget + Lewis = Android Lint CI

Integrating our Android linter with Github's pull requests

Aug 04 2016 : Fernando Ramirez and Euen Lopez

/
Passwordless login with phoenix

Introducing how to implement passwordless login with phoenix framework

Jul 27 2016 : Thiago Borges

/
Beam Olympics

Our newest game to test your Beam Skills

Jul 14 2016 : Brujo Benavides

/
Otec

Three Open Source Projects, one App

Jun 28 2016 : Andrés Gerace

/
CredoCI

Running credo checks for elixir code on your github pull requests

Jun 16 2016 : Alejandro Mataloni

/
See all Inaka's blog posts >>

/
Server Sent Events (SSE): EventSource implementation on Swift.

Andres Canal wrote this on May 28, 2015 under dev, eventsource, ios, open, server-sent-events, sse, swift .

We are pretty used to develop apps that consume REST APIs. We ask the server to perform an action and that's it. But this it's not always the best way to go in some scenarios. What happens if you need to get real time events from a server? The first thing that comes to our head is just set a timer and poll the server with a GET every 30 seconds or so, but this is not a good approach at all, lots of overhead, polling even if server has nothing to return and some other negative impacts on the server too.

So what's the way of doing this? The answer is Server Sent Events. This is an API standarized as part of HTML5 by the W3C. But if this is web technology why I'm talking about it on a iOS post? Because we can stil use it, we can consume those events, we just need to know how to do it or use a library that follows the standard.

Here you can read the latest library specification. Basically SSE works opening a connection and keeping that connection opened for ever, when the server has something to send it just sends the data over that connection. The stream of data follows a convention so everybody knows what to expect, if the connection drops you reconnect and that's it. Pretty easy.

Where can I apply this?

Well, the answer would be: 'every time you need to get data from a server not knowing when that data is going to be available'. For example a chat, you need to get the message when the message is sent, you don't want to have to ask the server for it, you want it now! Another example would be an app that tracks a vehicle in a map, you want to know where the vehicle is as the vehicle reports it's location.

How do I use it?

1- Download the library, drag the EventSource.swift file to your project or you can install this using cocoapods.

pod 'IKEventSource'

2- Create a new EventSource object:

var eventSource: eventSource = EventSource(url: server, headers: ["Authorization" : basicAuthAuthorization])

3- Subscribe to the different events:

eventSource.onOpen {
    // When opened
  }

  eventSource.onError { (error) in
    // When errors
  }

  eventSource.onMessage { (id, event, data) in
    // Here you get an event without event name!
  }

  eventSource.addEventListener("event-name") { (id, event, data) in
    // Here you get an event 'event-name'
  }

And that's it. You can close the connection when you don't need it anymore:

eventSource.close()

Checkout our github page for IKEventSource: IKEventSource