Skip to content

A react-native component for EventSource: Server-Sent Events for iOS and Android

Notifications You must be signed in to change notification settings

RamIdeas/react-native-eventsource

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm version

react-native EventSource

Server-Sent Events for your React Native apps!

EventSource implementation following the W3C EventSource specification.

NOTE: This is my first time playing with Obj-C and Java and the iOS and Android ecosystems. If you see something that makes you go 😦 please let me know. PRs are always welcome!

Installing

npm install react-native-eventsource --save

rnpm link react-native-eventsource

Usage

Full example that subscribes to a SSE stream and writes the results to console.log

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} = React;

var EventSource = require('react-native-eventsource');

var example = React.createClass({
  getInitialState() {
    return {
      source: null,
      url: 'YOUR_ENDPOINT',
    };
  },

  componentDidMount() {
    this.source = new EventSource(this.state.url);

    this.source.onopen = function () {
      console.log('EventSource::onopen');
    };

    this.source.onmessage = function (e) {
      console.log('EventSource::onmessage: ', e);
    };

    this.source.onerror = function (e) {
      console.log('EventSource::onerror: ', e);
    };

    this.source.addEventListener("custom_event", function(e) {
      console.log('EventSource::custom_event: ', e);
    };
  },

  componentWillUnmount() {
    this.source.close();
  },

  render: function() {
    return (<View><Text>SSE in React!</Text></View>);
  }
});

Thanks

@jordanbyron for the EventSource for react-native for iOS inspiration.

@neilco for the EventSource for iOS. @kaazing for the EventSource for Android.

License

See EventSource for additional license details.

See kaazing/java.client for additional license details.

Copyright (c) 2015 João Ribeiro (http://github.com/JonnyBGod/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A react-native component for EventSource: Server-Sent Events for iOS and Android

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 97.1%
  • Objective-C 2.1%
  • JavaScript 0.8%