json2021
json2021

Reputation: 2327

Socket.io not working on android react native

I am trying to socket.io to react native. It works perfectly fine on iOS device but doesn't work on android. I was doing some research and found out you need to usesCleartextTraffic="true".

I did that and its still not working. Below is my code. Any help would be really appreciated.

server code

const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer)

io.on("connection", (socket) => {
    socket.on("hello",function (){
        io.emit("hello", "worldd");
    })
});

httpServer.listen(3000);

Client Side

useEffect(()=> {


    try {
      const socket = io('http://localhost:3000');


      socket.on("hello", (param) => {
        console.log(socket.id); // x8WIv7-mJelg7on_ALbx
        console.log(param);
      });


    } catch (e) {

      console.log(e)

    }


  });

Upvotes: 1

Views: 960

Answers (2)

json2021
json2021

Reputation: 2327

I found out what the issue was. I had put an actual url not use localhost ans it worked. I used ngrok for that

Upvotes: 3

Mustafa
Mustafa

Reputation: 353

I think first you need to change the way you are setting up you server, change it to this instead as suggested by the documentation

const app = express();
const http = require('http');
const server = http.createServer(app);
const io = new Server(server);

The calling socket emit and on seems to be correct so try this and let me know if it works. you can also check full documentation here https://socket.io/get-started/chat

Upvotes: -1

Related Questions