Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket IO with ReactNative

I try to use SocketIO in ReactNative by follow this link https://github.com/facebook/react-native/issues/4393,
On IOS it work very well but Android it could not work Result Of Socket Object connected:false

index.android.js

window.navigator.userAgent = 'react-native';//'react-native';

const io = require('socket.io-client/socket.io');

export default class testApp extends Component {
    componentWillMount(){
        this.socket = io.connect('http://localhost:3000', {
            jsonp: false,
            transports: ['websocket'] 
        });
        // Socket Object connected:false
    }
    componentDidMount(){
        console.log(this.socket);
        this.socket.on('connect', () => {
            console.log('ready to emit')
            console.log('connected!');
        });
    }
    // Other Code
}

package.json:

"react-native": "0.35.0",
"socket.io-client": "^1.5.1"

I could not find a similar problem. Am I missing something?

edited : I'm not sure can I test socketIO in localhost with ReactNative but It's work when I test on IOS emulator

edited2 : My fault It cannot test on local environment server but It's work on IOS not android Can Anybody Explained Why?

like image 859
Terdtai Watanatien Avatar asked Oct 27 '25 10:10

Terdtai Watanatien


1 Answers

I also wanted to use Socket.IO with ExpressJS server and React Native but couldn't get it to work.

Then used https://facebook.github.io/react-native/docs/network.html#websocket-support with https://github.com/websockets/ws

And works great.

like image 63
Theo Bouwman Avatar answered Oct 30 '25 01:10

Theo Bouwman