Support Topics Documentation Slack YouTube Blog

Messaging Event Handlers

Hey, I’m trying to store the messages of all my channels to Backendless’s database, and I’ve been reading through documentation, but I haven’t really understand how to change my ‘afterPublish’ from draft.

I’ve got the code from the ‘Download’ but still don’t understand what to do with it. Does it go into my project’s code?

Any help would be appreciated,
Alex

@Alexandre_Coutinho Hi!
In the ‘afterPublish’ you have parameter Object message - so you receive text of message.
And inside handler you should add logic to save messages to database. If you still have a questions, please, provide some example and I will try to explain you step-by-step solution.

Have you checked this https://backendless.com/docs/bl-java/bl_customizing_and_running_code.html?

Hi Sofia,
Thank you for that, but my doubt is where to add the code. Should the ‘afterPublish()’ function be added to my code where the publish is first made, or do I need to add the ‘OrderTableEventHandler’ class with the ‘afterPublish()’ inside?

Alex

Look, event handlers are part of cloud code. So you need to create afterPublish handler (follow the documentation ), add your logic inside and deploy it. Class with handler should extends com.backendless.servercode.extension.MessagingExtender and override afterPublish method.

Also, do you really need to use event handler and maybe you can just save data to database inside your code after calling method Messaging.publish() or inside handleResponse (in AsyncCallback)

I’d prefer that yes! How can I do that?

Do I just call the afterPubish()?

Can you show me example of code where you call publish method?

Of course!

    message.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;

            if (actionId == EditorInfo.IME_ACTION_SEND || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {

                message.setEnabled(false);

                Backendless.Messaging.publish(channelName, wrapToColor("[" + name + "]") + ": " + message.getText().toString(), new AsyncCallback<MessageStatus>() {
                    @Override
                    public void handleResponse(MessageStatus response) {
                        Log.d(TAG, "Sent message " + response);
                        message.setText("", TextView.BufferType.EDITABLE);
                        message.setEnabled(true);
                    }

                    @Override
                    public void handleFault(BackendlessFault fault) {
                        message.setEnabled(true);
                    }
                });
                handled = true;
            }

            return handled;
        }
    });

So you can try something like this:

@Override
      public void handleResponse(MessageStatus response) {
        Log.d(TAG, "Sent message " + response);

        YourClassToSaveMessages messageObject = new YourClassToSaveMessages()
        messageObject.setText(message.getText());
        
        Backendless.Data.of(YourClassToSaveMessages.class).save(messageObject);
        
        message.setText("", TextView.BufferType.EDITABLE);
        message.setEnabled(true);
      }

With this done my chat is saved on the database?

This code would save text of the message to table with name YourClassToSaveMessages (to the column with name text), or you expect something else?

Doing that I’ll have a table with all the chat right? If I use different channels, and I want to retrieve the messages of only one chat, is there anything else I’ll need to do?

That’s what I’m trying to figure out now, but your help has been amazing.

Try to add column channel to table with messages (add property to class which you use ( YourClassToSaveMessages from my example)) and in this case if you need to receive messages depends on channel, just use find() and put channel = '<nameOfChannel>' to whereClause in query

1 Like

Thank you for the help Sofia, that was exactly what needed to be done.