Skip to content

Provide Swift LiveQuery use case example for Realtime Messaging #1197

@otymartin

Description

@otymartin

For implementation related questions or technical support, please refer to the Stack Overflow and Server Fault communities.

Make sure these boxes are checked before submitting your issue -- thanks for reporting issues back to Parse Server!

  • [Yes ] You've met the prerequisites.
  • [ Yes] You're running the latest version of Parse Server.
  • [ Yes] You've searched through existing issues. Chances are that your issue has been reported or resolved before.

Environment Setup

MongoLab(mLab), Google App Engine...
@nlutsenko @wangmengyan95

Since iOS docs aren't released yet i'm still struggling to implement live queries on the client side.
So I'd like to get an example for how it could be use to send/recieve realtime messages in a messaging app using a method I'm using in my own chat app which was previously using push notifications.

How could I implement liveQueries in this method to send/receive realtime messages?

// MARK: - LOAD MESSAGES
    func loadMessages() {

        print("Loading messages...")
        var lastMessage:JSQMessage? = nil

        if messages.last != nil {
            lastMessage = messages.last
        }

        let messageQuery = PFQuery(className: "Message")
        messageQuery.whereKey("room", equalTo: self.room!)
        messageQuery.orderByAscending("createdAt")
        messageQuery.limit = 500
        messageQuery.includeKey("user")

        if lastMessage != nil {
            messageQuery.whereKey("createdAt", greaterThan: lastMessage!.date)
        }

        messageQuery.findObjectsInBackgroundWithBlock { (results: [PFObject]?, error:NSError?) -> Void in

            if error == nil {

                if results != nil {

                    let messages = results

                    for message in messages! {

                        self.messageObjects.append(message)

                        let user = message["user"] as! PFUser
                        self.users.append(user)

                        let chatMessage = JSQMessage(senderId: user.objectId , displayName: user.objectForKey("name") as! String, text: message["content"] as! String)
                        self.messages.append(chatMessage)

                        Answers.logCustomEventWithName("Messages Loaded", customAttributes: nil)

                    }
                }

                if results?.count != 0 {
                    self.finishReceivingMessage()
                }

            }
        }

    }

Don't think my send method is needed but here it is either way.

   //MARK: - SEND MESSAGES
    override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {

        let message = PFObject(className: "Message")
        message["content"] = text
        message["room"] = self.room
        message["user"] = PFUser.currentUser()

        message.saveInBackgroundWithBlock { (success: Bool, error:NSError?) -> Void in

            if error == nil {

                self.loadMessages()
                //self.sendPushToContact()
                self.room!["lastUpdate"] = NSDate()
                self.room?.saveInBackgroundWithBlock(nil)
                Answers.logCustomEventWithName("Send Message", customAttributes: nil)

            } else {

                print("Error while sending message \(error?.localizedDescription)")
            }
        }
        self.finishSendingMessage()
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions