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.
Exceptions:
The wrapped libraries can be found here:
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();
Requires:
host
- example: 'localhost:3000'
Optional:
config
- JSON object comprising any of the options listed below.
Please note that the Android version does not support all of these options yet. It's very much a work in progress.
connectParams: Any Object
- Any data to be sent with the connection.
reconnects: Boolean
Default is true
reconnectAttempts: Int
Default is -1
(infinite tries)
reconnectWait: Number
Default is 10
forcePolling: Boolean
Default is false
. true
forces the client to use xhr-polling.
forceWebsockets: Boolean
Default is false
. true
forces the client to use WebSockets.
nsp: String
Default is "/"
. Connects to a namespace.
log: Bool
If true
socket will log debug messages. Default is false.
path: String
- If the server uses a custom path. ex: "/swift"
. Default is ""
cookies: [NSHTTPCookie]?
An array of NSHTTPCookies. Passed during the handshake. Default is nil.sessionDelegate: NSURLSessionDelegate
Sets an NSURLSessionDelegate for the underlying engine. Useful if you need to handle self-signed certs. Default is nil.connect
- Connect to socketon
- Add event handler to socket
@param
- String - event name@param
- Function - callbackonAny
- Add event handler to any event
@param
- Function - callbackemit
- Emit an event to server
@param
- String - Event name@param
- Anything - Data to be sentdisconnect
- Close the connectionreconnect
- Reconnect to a closed connectionjoinNamespace
- Manually join namespace
@param
- String - NamespaceleaveNamespace
- Leave namespace, back to '/'$ npm install react-native-socketio
In android/setting.gradle
...
include ':SocketIo'
project(':SocketIo').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-socketio/android')
In android/app/build.gradle
...
dependencies {
...
compile project(':SocketIo')
}
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!
);
}
......
}