Reputation: 1843
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]);
}
Upvotes: 2
Views: 3006
Reputation: 637
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);
});
}
Upvotes: 0
Reputation: 2832
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,
},
],
);
Upvotes: 2