Shubham
Shubham

Reputation: 1793

Not able to connect socket in react native

I'm trying to connect socket in react native. I am able to connect same socket in html file.

Server Code :

const express = require("express")
    , app = express()
    , http = require("http").createServer(app)
    , fs = require("fs")
    , io = require("socket.io")(http);


app.use(express.static("public"));

http.listen("8080", () => {
    console.log("Server Started");
});

function socketIdsInRoom(name) {
    var socketIds = io.nsps['/'].adapter.rooms[name];
    if (socketIds) {
        var collection = [];
        for (var key in socketIds) {
            collection.push(key);
        }
        return collection;
    } else {
        return [];
    }
}

io.on('connection', function (socket) {
    console.log('connection');

    socket.on('disconnect', function () {
        console.log('disconnect');
    });
});

React Native Code:

import io from 'socket.io-client';

const socket = io.connect('http://127.0.0.1:8080', { transports: ['websocket'] });

//const socket = io("http://127.0.0.1:8080"); /* I've also tried this */
socket.on('connect', function (data) {
    console.log('connect');
});

I'm using socket.io-client for socket connections in react native

Upvotes: 0

Views: 1118

Answers (1)

Metalik
Metalik

Reputation: 943

Problem is related to 127.0.0.1 ip. Android can't connect to 127.0.0.1 ip because it is internal loop back IP.

You should replace 127.0.0.1 with your server Ip ether external or internal IP.

Upvotes: 3

Related Questions