0

I am trying to connect node.js service which is working with socket.io. If I deployed to herokur, it works well. But I like to connect on local side while doing development. So here what point I am.

node.js;

"dependencies": {
    "express": "^4.17.1",
    "socket.io": "^2.3.0"
  }
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
  });
  
io.on('connection', function(socket){
    console.log('a user connected');

    //If the user left
    socket.on('disconnect', function(){
        console.log(' disconnected');
        socket.broadcast.emit('broadcast',this.name +' left the Room!');
    });

});

http.listen(3000, function(){
    console.log('listening on *:3000');
});

My basic flutter app to see connection on server side,

import 'package:flutter/material.dart';

import 'package:socket_io_client/socket_io_client.dart' as IO;
import 'dart:developer';

void main() async {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
     
        primarySwatch: Colors.blue,
      
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  void _connect() async {
    IO.Socket socket = IO.io('http://192.168.1.105:3000', <String, dynamic>{
    'transports': ['websocket'],
    'extraHeaders': {'foo': 'bar'} // optional
  });

    socket.on('connect', (_) {
      print('connect');
      socket.emit('chat message', 'from app wooow');
    });

    socket.on('data', (_) {
      print(_);
    });

    socket.on('connect_error', (data) => {print(data),log("connect_error")});
    socket.on('connect_timeout', (data) => {print(data),log("connect_timeout")});
    socket.on('connecting', (data) => {print(data),log("connecting")});
    socket.on('disconnect', (data) => {print(data),log("disconnect")});
    socket.on('error', (data) => {print(data),log("error")});
    socket.on('reconnect', (data) => {print(data),log("reconnect")});
    socket.on('reconnect_attempt', (data) => {print(data),log("reconnect_attempt")});
    socket.on('reconnect_failed', (_) => print(_));
    socket.on('reconnect_error', (_) => print(_));

    socket.on('reconnecting', (_) => print(_));
    socket.on('ping', (_) => print(_));
    socket.on('pong', (_) => print(_));
    log('called');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _connect();
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

I have tried to connect with,

http://10.0.2.2:3000,
http://127.0.0.1:3000,
http://localhost:3000,
http://myRouterIp:3000

and tried to turn of all network security. But I couldn't connect it from my real device on debug mode and from Android emulator.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Burak Duman
  • 35
  • 1
  • 8
  • Connecting your real device is not that straight forward. Checkout this post: https://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device. Regarding the android emulator issue, using 10.0.2.2 did the trick for me, maybe you'll find some help here: https://stackoverflow.com/questions/5528850/how-do-you-connect-localhost-in-the-android-emulator – orotype Apr 04 '21 at 16:33
  • I have spent all day and tried to do everything they suggested as you shared. And never connected and now I tried again after your answer as you said, Android emulator worked with 10.0.2.2. Thank you for your answer @orotype – Burak Duman Apr 04 '21 at 22:35

0 Answers0