Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Swift LiveQuery use case example for Realtime Messaging #1197

Closed
otymartin opened this issue Mar 25, 2016 · 1 comment
Closed

Provide Swift LiveQuery use case example for Realtime Messaging #1197

otymartin opened this issue Mar 25, 2016 · 1 comment

Comments

@otymartin
Copy link

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()
    }
@otymartin otymartin changed the title Provide Swift LiveQuery use example for Realtime Messaging Provide Swift LiveQuery use case example for Realtime Messaging Mar 25, 2016
@wangmengyan95
Copy link
Contributor

Hi @otymartin, we use this repo to track the parse-server related issues. Could you open the thread under our LiveQuery iOS client here. cc @richardjrossiii

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants