2

I am using socket_io_client package to communicate with nodejs socket server. Here is my socket configuration for flutter:

socket = IO.io(SocketUrl.url, <String, dynamic>{
      'transports': ["websocket"],
      "autoConnect": false
    });
    socket.connect();
    socket.on('connect', (_) {
      print('socket connected to server');
    });
    socket.on('newMessage', (message) {
      print(message);
    });
    socket.on('event', (data) => print(data));
    socket.on('disconnect', (_) => print('disconnect'));
  }

Here is logic for file sending:

const data = {
            userId: currentUser.id,
            file: file.name
          };
          const stream = ss.createStream();
          ss(socket).emit("send-file", stream, data);
          ss.createBlobReadStream(file).pipe(stream);

This is how it would be done from a node client. I need to write dart version of above code. I saw that quiver package has async functions to create streambuffer but I don't know how to implement that for this particular application.

Edited:

 void onSendFile(file, data) async {
    final isFile = File(file);
    var buffer = isFile.openRead();
    //socket.emit("send-file",[buffer,data]);
    socket.emitWithBinary("send-file", [buffer, data]);
  }

2 Answers 2

2
+50

Reading from the API references and issue on socket_io_client's repo, you should be able to send binaries with socket.emitWithBinary.

Assuming you already have a mean to get on a File, you'd most likely able to do this

final myFile = File('file.txt');
final bytes = await myFile.readAsBytes();

socket.emitWithBinary('send-file', bytes);

Update

From the JS version of socket-io client, the emit function is able to take multiple arguments, which in the end is converted to an array, and so I presumed that you'll be able to achieve the same thing by passing an array to emitWithBinary or emit

Note

Seeing that your JS version doesn't contain any binary flags, you might want to try emit instead.

socket.emit(
  'send-file',
  [
    123,
    {
      'userId': currentUser.id,
      'file': file.name,
    },
  ],
);
Sign up to request clarification or add additional context in comments.

7 Comments

Even if I do that, there are three parameters to be passed. I still have to send data. Doing this, I might be able to send file but it doesn't take third parameter i.e data. And json Encoding third parameter especially with bytes doesn't work.
Updated the answer. Note that I did not actually test on it, I'm merely scanning through the source codes of JS and Dart version of the socket-io implementation
Thank you for going through all that. I tried above solution and it didn't work out for me. I am merely trying to accomplish file sharing through already created chat platform on web using node js. I was told that the data would be of type 'buffer' and stream used was duplex stream.
Just making sure if you've tested on emitWithBinary. If it's a stream/buffer you want, try reading the file with openRead
I have added my code to the question. That's what I have tried so far.
|
0

You can use package https://pub.dev/packages/web_socket_channel
See an Official example https://flutter.dev/docs/cookbook/networking/web-sockets
convert file to bytes and sink to the stream

final bytes = await myFile.readAsBytes();

main() async {
  var channel = IOWebSocketChannel.connect("ws://localhost:1234");
  channel.stream.listen((message) {
    channel.sink.add(bytes);
    channel.sink.close(status.goingAway);
  });
}

1 Comment

Thanks for the suggestion but I tried all socket libraries and only socket_io_client worked for me. Using other library, I couldn't even connect to the socket url for some reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.