gcrabtree / react-native-socketio

Not Maintained! A React Native wrapper for both the Swift and Java Socket.Io clients.
MIT License
152 stars 53 forks source link

A React Native wrapper for the Socket.io Library

Please note that this hasn't been maintained for ages. If someone would like to bring back upto speed, let me know. -Greg

This project was forked from Kirkness' React Native Swift Socket.Io project found here

Note: We are working on the following items:

This project now supports both iOS and Android using the same JS calls.

Example

Kirkness added a super simple example app to /examples, copy and paste to your index.ios.js.

/**
 * Pass in an optional config obj, this can include most of the
 * standard props supported by the swift library
 */
var socketConfig = { path: '/socket' };
var socket = new SocketIO('localhost:3000', socketConfig);

// Connect!
socket.connect();

// An event to be fired on connection to socket
socket.on('connect', () => {
    console.log('Wahey -> connected!');
});

// Event called when 'someEvent' it emitted by server
socket.on('someEvent', (data) => {
    console.log('Some event was called, check out this data: ', data);
});

// Called when anything is emitted by the server
socket.onAny((event) => {
    console.log(`${event.name} was called with data: `, event.items);
});

// Manually join namespace. Ex: namespace is now partyRoom
socket.joinNamespace('partyRoom')

// Leave namespace, back to '/'
socket.leaveNamespace()

// Emit an event to server
socket.emit('helloWorld', {some: 'data'});

//Disconnect from server
socket.disconnect();

// Reconnect to a closed socket
socket.reconnect();

Constructor

Requires: host - example: 'localhost:3000'

Optional: config - JSON object comprising any of the options listed below.

Config

Not supported yet but easy enough to implement.

Methods

Known issues

Install

iOS

Android

  1. In android/setting.gradle

    ...
    include ':SocketIo'
    project(':SocketIo').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-socketio/android')
  2. In android/app/build.gradle

    ...
    dependencies {
        ...
        compile project(':SocketIo')
    }
  3. Register module (in MainActivity.java)

    import com.gcrabtree.rctsocketio.SocketIoPackage;  // <--- import
    
    public class MainActivity extends ReactActivity {
      ......
    
      @Override
      protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new VectorIconsPackage(),
          new OrientationPackage(this),
          new SocketIoPackage()   // <--- Add here!
      );
    }
    
      ......
    
    }