title: My app in Flutter integrates a chat with Socket.io and does not connect to a remote server
body: <p>Develop a flutter application that includes a chat that uses Socket.io</p>
  <p>The server is hosted by an EC2 instance on AWS.  I did a test first on my local computer.</p>
  <p>Create a server with socket.io and a web client to test the connection, It worked fine.</p>
  <p>Server code:</p>
  <pre><code>const path = require(&quot;path&quot;);
  const http = require('http');
  const express = require(&quot;express&quot;);
  const socketio = require(&quot;socket.io&quot;);
  const app = express();
  
  
  
  app.set('port', 3000);
  
  //static files
  
  app.use(express.static(path.join(__dirname, &quot;public&quot;)));
  
  //start the server
  const server = app.listen(app.get('port'), () =&gt; {
  
      console.log(&quot;server on port&quot;, app.get('port'));
  
  });
  
  const server2 = http.createServer(server);
  
  const io = socketio(server);
  
  //websockects
  
  io.on('connection', (socket) =&gt; {
  
  
      console.log(&quot;new connection  &quot; + socket.id);
      console.log(socket);
  
  
      socket.on('chat:message', (data) =&gt; {
          console.log(data.constructor.name);
          console.log(data);
  
  
          io.sockets.emit(&quot;chat:server&quot;, data);
  
      });
  
      socket.on('chat:typing', (data) =&gt; {
          console.log(data);
  
          socket.broadcast.emit(&quot;chat:typing&quot;, data);
  
      });
  
      socket.on('message', (data) =&gt; {
          console.log(data.constructor.name);
          console.log(data);
  
  
          io.sockets.emit(&quot;news&quot;, { respuesta: &quot;Recibio&quot; });
  
      });
  });
  </code></pre>
  <p>Client code:</p>
  <pre><code>///coneccion al servidor
  const socket = io();
  
  //dom elements
  let message = document.getElementById(&quot;message&quot;);
  let username = document.getElementById(&quot;username&quot;);
  let btn = document.getElementById(&quot;button&quot;);
  let output = document.getElementById(&quot;output&quot;);
  let actions = document.getElementById(&quot;actions&quot;);
  
  //evento de envio de mensaje
  btn.addEventListener('click', function() {
      console.log({
          username: username.value,
          message: message.value
      });
      //socket que emite el mensaje al servidor
      socket.emit(&quot;chat:message&quot;, {
          username: username.value,
          message: message.value
      });
  });
  
  
  //Escuchador de teclado, se activa cuando el usuario teclea
  message.addEventListener('keypress', function() {
      //emite  el nombre del usuario cuando este esta teclando al lado del cliente
      socket.emit(&quot;chat:typing&quot;, username.value);
      socket.emit(&quot;message&quot;, username.value);
  });
  
  //socket que recibe los mensajes del servidor
  socket.on(&quot;chat:server&quot;, function(data) {
      console.log(data);
      actions.innerHTML = &quot;&quot;;
      output.innerHTML += &quot;&lt;p&gt;&lt;strong&gt;&quot; + data.username + &quot;&lt;/strong&gt;: &quot; + data.message + &quot; &lt;/p&gt;&quot;;
  
  });
  
  //socket que recibe el nombre del usuario que esta teclando
  socket.on(&quot;chat:typing&quot;, function(data) {
      console.log(data);
      actions.innerHTML = &quot;&lt;p&gt;&lt;strong&gt;&quot; + data + &quot;&lt;/strong&gt; esta escribiendo &lt;/p&gt;&quot;;
  
  });
  
  socket.on(&quot;news&quot;, function(data) {
      console.log(data);
  
  });
  </code></pre>
  <p>After this, I uploaded the server code to the EC2 instance.  The server uses NGINX, I had to modify it so that it accepted connections from outside the server, like this:</p>
  <pre><code>Access-Control-Allow-Origin: *
  </code></pre>
  <p>I just modify the connection socket of the client:</p>
  <pre><code>const socket = io(&quot;https://mycustomdomain.org&quot;);
  </code></pre>
  <p>And worked, from my pc I connect to the server.  Now my problem is with flutter. I tried with two libraries:</p>
  <p>-https://pub.dev/packages/socket_io_client
  -https://pub.dev/packages/adhara_socket_io</p>
  <p>With neither of the two libraries I can connect to the server.     In localhost it worked but when i Tried to connect to EC2 server i cant connect.</p>
  <p>I made the simple example of each library,</p>
  <p>With Adhara:</p>
  <pre><code>Future&lt;void&gt; socketConfig() async {
          SocketIOManager manager = SocketIOManager();
          SocketIO socket = await manager.createInstance('https://mycustomdomain.org');       //TODO change the port  accordingly
          socket.onConnect((data){
            print(&quot;connected...&quot;);
            print(data);
            socket.emit(&quot;message&quot;, [&quot;Hello world!&quot;]);
          });
          socket.on(&quot;news&quot;, (data){   //sample event
            print(&quot;news&quot;);
            print(data);
          });
          socket.connect();
          ///disconnect using
          ///manager.
      }
  
      socketConfig();
  </code></pre>
  <p>And with socket.io-client</p>
  <pre><code>import 'package:socket_io/socket_io.dart';
  import 'package:socket_io_client/socket_io_client.dart' as IO;
  
  main() {
      // Dart server
      var io = new Server();
      var nsp = io.of('/some');
      nsp.on('connection', (Socket client) {
        print('connection /some');
        client.on('msg', (data) {
          print('data from /some =&gt; $data');
          client.emit('fromServer', &quot;ok 2&quot;);
        });
      });
        io.on('connection', (Socket client) {
          print('connection default namespace');
          client.on('msg', (data) {
            print('data from default =&gt; $data');
            client.emit('fromServer', &quot;ok&quot;);
          });
        });
        io.listen(3000);
  
      // Dart client
      IO.Socket socket = IO.io('https://mycustomdomain.org/');
      socket.onConnect((_) {
       print('connect');
       socket.emit('msg', 'test');
      });
      socket.on('event', (data) =&gt; print(data));
      socket.onDisconnect((_) =&gt; print('disconnect'));
      socket.on('fromServer', (_) =&gt; print(_));
  }
  </code></pre>
  <p>It didn't even manage to detect them connecting to the server.  By the way, my domains has SSL certificates.</p>
  <p>Any idea what might be going on?</p>