Reputation: 85
I am creating a react native app and and I'm trying to connect my client app to my server. I am using socket-io and I'm unsuccessful in connecting the two so far. Here is my code on the client:
import SocketIOClient from 'socket.io-client';
(...)
if (result.type === "success") {
console.log("Here 1");
this.socket = SocketIOClient('http://localhost:1997');
this.socket.emit('test', 'Hello world!');
console.log("Here 2");
}
And this code outputs "Here 1" "Here 2"
My Server code is as following:
let express = require('express');
let http = require('http')
let socketio = require('socket.io');
var app = express();
var server = http.Server(app);
var websocket = socketio(server);
let port = 1997;
server.listen(port, () => console.log('listening on *:' + port));
websocket.on('connection', (socket) => {
console.log("Client joined on " + socket.id);
websocket.on('test', function(data){
console.log('Data Receieved: ' + data);
});
});
This code outputs "listening on *:1997" and then nothing else even when the client code runs. Please help I'm not sure what I'm doing wrong.
Upvotes: 0
Views: 1243
Reputation: 416
I had the same issue and changing "localhost" to the actual ip, worked like a charm for me.
So change
this.socket = SocketIOClient('http://localhost:1997');
To
this.socket = SocketIOClient('http://192.168.xxx.xxx:1997');
I got the answer from here: Using Socket.io with React Native and not working
Upvotes: 1