Write Server-Side Cloud Code with the Swift SDK

Version 4.6.0 of the Swift SDK allows you to write Parse Cloud Code Functions and Triggers completely in Swift by taking advantage of Parse Webhooks.

Using the Swift SDK on the server-side allows you to send Push Notifications, create/modify schema’s, delete files, etc. To quickly get a Swift server that can connect to your nodejs Parse Server up and running quickly, fork the repo:

Have fun writing Swift Cloud Code!

4 Likes

I did not even know this was possible. Very impressive!! :clap: :clap:

1 Like

Hi @cbaker,

This looks like a great addition, could you please elaborate more about how this fits in with the existing parse server?

From looking at the sample code, if I understand things correctly you spin up vapor https server, integrate with parse swift sdk to handle certain routes, and then on the client side use the parse swift sdk to invoke those cloud functions.

Is my understanding correct? What about users and authentication and database storage?

Sorry for maybe a beginner question, just want to understand where this piece of puzzle fits…

Thanks,
Martin

Your understanding is correct! To add that it doesn’t matter which SDK you use on the client side because ParseServerSwift hooks into the nodejs Parse Server and never talks directly to client apps, only the nodejs Parse Server. You use it exactly the same way as you would a Cloud Code main.js file (with the exception of jobs at the moment). ParseServerSwift can easily be spun up on macOS directly, Linux directly, or a docker image.

I think a good explanation is buried in the original Parse docs as I believe this was the original intention for all of the SDKs assuming they are able to be spun up on a server like the JS SDK and now Parse-Swift;

Webhooks allow you to write your server-side logic in your own environment with any tools you wish to use. This can be useful if you want to use a language other than JavaScript, host it yourself for improved testing capabilities, or if you require a specialized library or technology not available in Cloud Code.

This doesn’t replace the nodejs Parse Server as it hooks into it, but “can” replace the majority of your Clode Code written in JS or work alongside it as Cloud Code JS and Clode Code Swift can co-exist. The nodejs Parse Server is responsible for making calls to the routes on ParseServerSwift and waiting for them to complete. This is the same process that occurs on Clode Code on the nodejs Parse.Server except it’s making calls to local express routes instead external routes on another server.

2 Likes

Thanks @dblythy! I’m hoping the strongly typeness of the Swift SDK can help developers improve their apps. There’s a couple problems I’ve been facing which I think are server-side that you may know how to address quickly. Can you let me know your thoughts;

  1. I can’t get ParseFile hook triggers to fire. I believe I’m passing the correct className, @File, but no luck. I know you worked on 7966 so you have a good idea on how the triggers work. The SDK is using REST to go through HooksController and using @Connect works for beforeConnect. It’s possible my test parse server is causing the issue for me as I haven’t tried the JS Cloud Code for a ParseFile on it to compare. You can see the json below:
  1. Updating Hooks appears to have a small bug I haven’t reported yet. Basically when updating a hook it never replaces/deletes the original hook. Not sure if the server actually uses the newest hook with the same name, I assume it doesn’t because if you fetch the hook it only returns the original. If you fetchAll, it returns all of the created hooks, previous and updated. The problem seems to be in updateHook below. In the “if (result)” blocks it should probably delete the result first and then call createOrUpdateHook:

In addition, the testcase below can be improved to update the hook twice and check the count for 1 hook before removing as the test looks like it’s currently not capturing some corner cases:

What do you think?

Sorry @cbaker6, I didn’t get the chance to look at this. I’m not quite familiar to how the hook update API works. Did you still need assistance? I can dive in and have a look this week