juho-jaakkola / elgg-pusher

3 stars 1 forks source link

client originated messages handling is not supported #1

Open roybirger opened 8 years ago

roybirger commented 8 years ago

Client originated messages are currenly sent to all other subscribers which is not very helpful if you want to do something else with the message (for example trigger some server action). It would be great if they could be handled by an external component or plugin.

juho-jaakkola commented 8 years ago

I have two approaches in mind:

  1. The push server takes Elgg engine as a parameter, so it can trigger Elgg's internal features
  2. Each client originated message would be passed to Elgg using a separate AJAX call
roybirger commented 8 years ago
  1. I think that injecting the engine would not help because it will mean that custom logic will have to be introduced to the plugin which will not be communication related.
  2. A seperate call would also be quite problematic if we want actions that are a direct result of a client mesage since it would mean that you would have to store client states or some similar data.

How about:

  1. An interface for commands that would be triggered by the plugin on messages' arrival?
juho-jaakkola commented 8 years ago
  1. The engine could provide just the events service (and/or plugin hook service). That way the actual logic could be built into custom Elgg plugins instead of the communications plugin.
  2. I'm not sure what problems you mean. I think we could just make an ajax call to an Elgg action and on success:
    • Update the originator's DOM accordingly when the server responds
    • Communicate the action to other connected clients through ZMQ
  3. How would these be communicated to Elgg, if Elgg core is not injected to the push server?

(I numbered the suggestions on the previous messages, so it's easier to follow the discussion.)

roybirger commented 8 years ago

Which hooks would the plugin trigger in case the elgg engine is passed to it? (Sorry but I'm relatively new to elgg...) Would these be custom hooks or using one of the built in hooks?

juho-jaakkola commented 8 years ago
  1. The pusher plugin could trigger its own custom events.
// Called by the push server when it receives a client-originated message
elgg_trigger_event('receive', 'message', $message);
// Third-party plugins can now register an event handler for the [receive, message]
// event and do what they want with the message.
elgg_register_event_handler('receive', 'message', function($event, $type, $message) {
    // Do something with the message. E.g. save it to database:
    $chat_message = new ChatMessage();
    $chat_message->content = $message->content;
    $chat_message->save();
});
roybirger commented 8 years ago

Looks good. I'll try to incorporate this into my project and use this plugin for communication,

If you need help with the work let me know.

juho-jaakkola commented 8 years ago
  1. One risk in this approach is that the Elgg core isn't really meant to run as an ongoing process (as it would if it were injected to the push server). There may be some memory leaks.
  2. Out of curiosity, could you explain in more detail the problems that this approach would cause? I see that it could work something like this:
    • User makes a call using AJAX
    • Server saves the data to database
    • Server sends the data to ZMQ (example here)
    • ZMQ sends the data to other connected users (example here and here)
roybirger commented 8 years ago

In my login flow I want to send the user all of his pending notifications once he connects.

I also send a callback ID with each request in order to resolve the correct data in the client and in addition I save the latest callback id in the plugin (by assuming a maximum of one pending request to the server)

If I'll have to break this flow It will mean that I have to save to order of messages or keep several callbacks at any given time which I would like to avoid.

roybirger commented 8 years ago

Hi, Are you planning to add the custom hook trigger on message to the pusher plugin? Also, can the pusher plugin avoid sending every message to all of the online users somehow?

juho-jaakkola commented 8 years ago

Are you planning to add the custom hook trigger on message to the pusher plugin?

Yes, it is on my TODO list. There aren't currently any clients paying for developing the plugin, so unfortunately it's a quite low priority. I'll however try to get the change in as soon as possible.

Also, can the pusher plugin avoid sending every message to all of the online users somehow?

You mean the message that tells which other users are online? AFAIK it's not send every message to all users. ...or is it?

juho-jaakkola commented 8 years ago

Please try out this branch: https://github.com/juho-jaakkola/elgg-pusher/tree/plugin_hook

When the server receives a message from client, it now triggers the [message, pusher] hook. Plugins can then register callbacks for the hook.

Would this help with your use case?