react-native-watch-connectivity icon indicating copy to clipboard operation
react-native-watch-connectivity copied to clipboard

Feature/implement receiving files

Open walterholohan opened this issue 3 years ago • 14 comments

This PR attempts to create the mechanism for the phone to receive a file from the watch using the didReceiveFile event. I have tried to copy how it was previously done for didReceiveUserInfo i.e. creating a fileQueue etc

walterholohan avatar Jul 12 '22 20:07 walterholohan

We don't use file transfers in that direction, so I'm unable to test your changes.

Code looks good to me.

yolpsoftware avatar Jul 12 '22 21:07 yolpsoftware

@walterholohan I'm thinking it might be worth adding this functionality to the example app - what do you think? That way e.g. @yolpsoftware would be able to test this out despite not having a need for it in their own app.

That being said - not sure how well the example app works on more recent RN versions, have either of you tried it recently?

mtford90 avatar Jul 13 '22 06:07 mtford90

@walterholohan I'm thinking it might be worth adding this functionality to the example app - what do you think? That way e.g. @yolpsoftware would be able to test this out despite not having a need for it in their own app.

That being said - not sure how well the example app works on more recent RN versions, have either of you tried it recently?

Yeah, I tried to run the example app yesterday but it was complaining about Flipper. I am going to test this new feature out on our production app today, as we have a time-critical bug that needs to be fixed asap.

At the moment when a user completes a workout on our apple watch app, we store a tonne load of data points from that workout, and at the need when a user saves it we try to send it back to the phone using user-info. However, we are getting a lot of crashes with the error message PayloadTooLarge. Our plan to fix this and make the transfer of data more reliable is to save the data to a JSON file and compress it on the watch using the zlib algorithm. We are then going to send it back to the phone using the file transfer API where the phone will then receive and upload the compressed file to our servers using a multipart/formdata POST request. We will then decompress the file using the node zlib library.

I will let you know how we get on with testing this approach today. Hopefully, once everything is working I will then have some time to update the Example app.

walterholohan avatar Jul 13 '22 07:07 walterholohan

@walterholohan I'm thinking it might be worth adding this functionality to the example app - what do you think? That way e.g. @yolpsoftware would be able to test this out despite not having a need for it in their own app. That being said - not sure how well the example app works on more recent RN versions, have either of you tried it recently?

Yeah, I tried to run the example app yesterday but it was complaining about Flipper. I am going to test this new feature out on our production app today, as we have a time-critical bug that needs to be fixed asap.

At the moment when a user completes a workout on our apple watch app, we store a tonne load of data points from that workout, and at the need when a user saves it we try to send it back to the phone using user-info. However, we are getting a lot of crashes with the error message PayloadTooLarge. Our plan to fix this and make the transfer of data more reliable is to save the data to a JSON file and compress it on the watch using the zlib algorithm. We are then going to send it back to the phone using the file transfer API where the phone will then receive and upload the compressed file to our servers using a multipart/formdata POST request. We will then decompress the file using the node zlib library.

I will let you know how we get on with testing this approach today. Hopefully, once everything is working I will then have some time to update the Example app.

Understood - sounds good. Not the end of the world if it doesn't make it to the example app - more of a nice to have.

mtford90 avatar Jul 14 '22 07:07 mtford90

@yolpsoftware @mtford90 we managed to get this working. One of the major hurdles that we faced was that the file was transfer to phone but was stored in temp storage on the FS and gets wiped once the function didReceiveFile returns. So in order to fix this, we are saving the file to a directory called FilesReceived within the Documents directory. It will then be up to the RN side to delete this file from FS when you don't need it anymore. In our case, we delete the file once we have successfully uploaded it to our servers. We also have a check on app load that will search this directory for any files that failed to upload and will then try retry the upload

image

walterholohan avatar Jul 14 '22 09:07 walterholohan

We also added in a listener for EVENT_WATCH_FILE_ERROR

walterholohan avatar Jul 14 '22 09:07 walterholohan

Hey @yolpsoftware @mtford90, would I be able to get another review on the above? I have added some more error listener for the below events which should help debug issues with connectivity. Also, just an FYI we have gone live with the file transfer approach and its all working great

EVENT_ACTIVATION_ERROR EVENT_APPLICATION_CONTEXT_RECEIVED_ERROR EVENT_SESSION_BECAME_INACTIVE EVENT_SESSION_DID_DEACTIVATE

walterholohan avatar Jul 20 '22 07:07 walterholohan

@mtford90 @yolpsoftware just bumping this. These changes have been live on our app in production for the last two weeks and we've experienced no issues so far

walterholohan avatar Aug 04 '22 05:08 walterholohan

@walterholohan sorry I have been away two weeks.

I have not really time to test this, but I'm glad to give my thumbs up if you say you are productively using these changes.

Two things:

  • I noticed you added the dist folder. Are those files auto-generated, or why weren't they necessary before this PR?
  • I'm not listed as a reviewer. Can I review anyway?

yolpsoftware avatar Aug 04 '22 10:08 yolpsoftware

@walterholohan sorry I have been away two weeks.

I have not really time to test this, but I'm glad to give my thumbs up if you say you are productively using these changes.

Two things:

  • I noticed you added the dist folder. Are those files auto-generated, or why weren't they necessary before this PR?
  • I'm not listed as a reviewer. Can I review anyway?

Thanks @yolpsoftware

Regarding the dist folder, its prob one for @mtford90 . I think he generates a new dist folder when deploying a new release to NPM. I was using it so I could use my fork in package.json and the app would pull the code from the dist folder

walterholohan avatar Aug 04 '22 15:08 walterholohan

Ah I see. I was missing that dist folder when I tried to use my fork on package.json. To me it makes sense to include it.

What do you think @mtford90 ?

yolpsoftware avatar Aug 04 '22 15:08 yolpsoftware

Thanks guys, I have now removed the dist folder

walterholohan avatar Aug 09 '22 07:08 walterholohan

Hey guys, just wanted to say a quick thanks, this works perfectly!

TamasNo1 avatar Aug 23 '22 18:08 TamasNo1

@mtford90 would it be possible to get a review on this PR and then get a new release?

walterholohan avatar Sep 14 '22 08:09 walterholohan

@walterholohan all done - sorry for delay

mtford90 avatar Sep 29 '22 06:09 mtford90