socket.io-client-dart icon indicating copy to clipboard operation
socket.io-client-dart copied to clipboard

Socket client is not emitting inside compute method in Flutter.

Open MilindModi opened this issue 2 years ago • 0 comments

I'm trying to implement socket client connection in background using compute method of flutter. compute method with socket is working on Node.js local server and successfully emitting all the events in background. Since i hosted my Node.js socket server, client is not able to emit or listen any event. I can see logs that it's connected and i can log the Socket id as well. I'm not sure what is going wrong here.

calling of BG method compute(DummyBG,DummyArgs(message:widget.arguments.message,sender_id:widget.arguments.user_id));

DummyBG

Future forwardMessageBG(ForwardArgs args) async {
 SocketUtils _socketUtils = SocketUtils();
  
 await _socketUtils.initSocket(args.sender_id, args.user.user_id);
 await _socketUtils.connect();

 _socketUtils.socket.emit("message", [message.toJson()]);
}

SokcetUtils

class {
  var user_id;
  var recid;
  initSocket(String user_id, String reciever_id) async {
    user_id = user_id;
    recid = reciever_id;
    this._fromUser = user_id;
    await _init();
  }

 _init() async {     
   socket = IO.io(
    deployment ? _serverIP : SERVER_CHAT,
    IO.OptionBuilder()
        .enableForceNew()
        .setTransports([
          'websocket'
        ]) // for Flutter or Dart VM// disable auto-connection//
        .setQuery({
          "info": _fromUser,
        })
        .setPath(deployment ? "/chat" : "/socket.io")
        .disableAutoConnect() 
        .build());
  }
} 

nginx confingured as below.

server {
        location / {
                proxy_pass http://localhost:3000/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade' ;
                proxy_set_header Host Shost;
                proxy_cache_bypass $http_upgrade;
        }
        location /chat/ {
                proxy_pass http://localhost:3001/socket.io/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade' ;
                proxy_set_header Host Shost;
                proxy_cache_bypass $http_upgrade;
        }
    server_name api.axn.in; # managed by Certbot
    proxy_read_timeout 600s; # added for user content upload
    client_max_body_size 200m; # added for user content upload
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/api.axn.in/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/api.axn.in/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = api.axn.in) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    server_name api.anx.in;
    listen 80;
    return 404; # managed by Certbot
}

Also posted this on Stackoverflow Found the similar Issue with no solution #91

MilindModi avatar Nov 28 '22 10:11 MilindModi