node.jsfluttersocket.io

How to secure socket.io using Nodejs and Flutter


I developed app using Nodejs and Flutter with socket.io client. I want to secure the Socket.io during the communication between two users. Any guide or suggestion pls? Thanks.


Solution

  • On Flutter, you can an extra header to your option headers. 'extraHeaders': {HttpHeaders.authorizationHeader: '$accessToken'}

    import 'package:socket_io_client/socket_io_client.dart' as IO;
    
    IO.Socket createSocket(String accessToken) {
      final url = 'https://$yourDomainHere';
      return IO.io(url,
        <String, dynamic>{
          'transports': ['websocket'],
          'rejectUnauthorized': false,
          'autoConnect': false,
          'extraHeaders': {HttpHeaders.authorizationHeader: '$accessToken'}
        });
    }