eBay / NMessenger

A fast, lightweight messenger component built on AsyncDisplaykit and written in Swift
Other
2.42k stars 272 forks source link

[readme] Better description of integration with messaging service #95

Open dodikk opened 7 years ago

dodikk commented 7 years ago

As a new user, I can't figure out which method is responsible for handling user's input. It's not quite clear where to put the networking code invocation. Is it sendText ? Oh, I'm subclassing the NMessengerViewController.

Ideally, I'd like to have the following topics documented :

  1. Where to invoke send message to network action ?
  2. How to handle the message received from network event properly?
  3. How to interact with the service while doing a Head Prefetching ?
dodikk commented 7 years ago

So, in order to send a message, I have to make an override :

// Declared in NMessengerViewController
//
 override func sendText(_ text: String, isIncomingMessage:Bool) -> GeneralMessengerCell
    {
        let shouldSendToServer = !isIncomingMessage
        if (shouldSendToServer)
        {
            // trigger network service
            self.controller?.sendMessageAsync(text)
        }

        // otherwise - just render 
        return super.sendText(text, isIncomingMessage: isIncomingMessage)
    }
dodikk commented 7 years ago

Here is a take on error handling https://github.com/eBay/NMessenger/issues/25 Haven't tried it yet.

dodikk commented 7 years ago

Receiving a message from the network service :

    func chatDidReceiveMessages(_ messageList: ChatMessageList)
    {
        messageList.forEach
        {
            _ = self.sendText($0.text, isIncomingMessage: true)
        }
    }
dodikk commented 7 years ago

I'm going to prepare a PR with readme changes these days.

atainter commented 7 years ago

Thank you. I'll merge it in.

dodikk commented 7 years ago

@atainter , I still have some difficulties with the understanding of "async head prefetching". I'd like to cover this topic in my PR as well. Could you explain in a bit more details, please? https://github.com/eBay/NMessenger/issues/97

As a rule, a chat application involves the following actions :

  1. I have to request the history and subscribe to incoming chat messages in ViewDidLoad.
  2. As new messages arrive I should invoke "sendText()" or other "send__" functions on NMessengerViewController simultaneously (in order to not miss any messages).

I have not figured out yet how to insert the bubbles for past messages to the top. Is "clearALLMessages()" and invoke "send___()" in a loop the only way for doing that? Is it a correct approach? Does NMessenger take care of the unintended "blinking" animation in such case?

My use case might even require inserting messages in the middle of the conversation. Several thousand users per room (text conference) are expected.

So the idea is

Showing "latest N" messages as a new batch arrives and ignore the rest of the batch until the user scrolls up to see those. 

Not sure how to implement that with NMessenger although it seems promising as it's built on top of AsyncDisplayKit.

dodikk commented 7 years ago

@atainter , the README has been updated. https://github.com/eBay/NMessenger/pull/114 Not sure how to complete history related part: I don't like "purge everything" approach but I have not figured out the alternative.

P.S. sorry for such a delay making this PR.

dodikk commented 7 years ago

Not sure how to complete history related part: I don't like "purge everything" approach but I have not figured out the alternative.

Actually, it's possible to make a custom insertion using the method below.

super.messengerView.addMessages(_,
                                atIndex:,
                        scrollToMessage:,
                              animation:,
                             completion:)

The method is used as a part of batch fetching implementation. https://github.com/eBay/NMessenger/blob/master/nMessenger/Source/Messenger/Components/NMessenger.swift#L203

kurry421 commented 6 years ago

@dodikk @atainter is it possible to send a typed message withOUT rendering on the ui?.. IE I just want to update the DB when a message is typed


 override func sendText(_ text: String, isIncomingMessage:Bool) -> GeneralMessengerCell
    {
        let shouldSendToServer = !isIncomingMessage
        if (shouldSendToServer)
        {
            // trigger network service
            self.controller?.sendMessageAsync(text)
        }
       // otherwise - just render (To NOT DO This part)
        return super.sendText(text, isIncomingMessage: isIncomingMessage)
}
dodikk commented 6 years ago

is it possible to send a typed message withOUT rendering on the ui

Override the function below and do not call super.

open override func onSendButtonTapped(havingText currentText: String)
dodikk commented 6 years ago

Still, It might be a good idea to render the message before receiving an xmpp delivery confirmation to provide the user with some feedback.