Search code examples
swifttriggersdigital-oceanparse-cloud-codeparse-server

Triggers in Parse Server using Swift


Recently, I was tasked to do a simple chat app for iOS, using Swift.. So, I have a parse server ready and running! All I want to know, is how to use triggers..

Let's say I have opened a conversation and I just received a new message. How can I get it, without constantly checking for new messages? I saw that cloud code is probably the way to go, but if it is so, is it practical? I mean, if I have 5000 users and they are constantly chatting, will it perform well?

Thanks in advance!


Solution

  • You want to use Parse LiveQuery component.

    1. Add Live Query to your server's config:

      let api = new ParseServer({
        ...,
        liveQuery: {
          classNames: ['Test', 'TestAgain']
        }
      });
      
      // Initialize a LiveQuery server instance, app is the express app of your Parse Server
      let httpServer = require('http').createServer(app);
      httpServer.listen(port);
      var parseLiveQueryServer = ParseServer.createLiveQueryServer(httpServer);
      
    2. Install Parse LiveQuery library as a pod to your project (pod 'ParseLiveQuery').

    3. Subscribe for events:

      let myQuery = Message.query()!.where("user", equalTo: PFUser.currentUser()!)
      let subscription: Subscription<Message> = myQuery.subscribe()
      
    4. Handle events:

      subscription.handleEvent { query, event in
          // Handle event
          // This callback gets called every time an object is created, updated, deleted etc.
      }