Jump to content

IC5: Introduction to Listeners

I'm very excited to be posting my first blog entry for IPS, and thrilled that this is what I get to post.

When we started planning the new development tools for v5, we looked at existing resources - from the marketplace, from some contributors, and from our own managed clients - and asked, what is it that developers find themselves doing the most often? Matt’s previous blog entry gave a brief summary of some of those functions. Today’s blog entry is going to focus on one of our powerful new tools, Listeners.

One common reason for a code hook was to execute custom code when a specific action occurs. For example, when a topic is created, or when a new reply is posted. Listeners allow you to execute your code at key points in the workflow, without the worry of finding exactly the right hook location.

Here is an excerpt from the BlogComment listener in our Cloud application:

Could contain: Electronics, Screen, Computer Hardware, Hardware, Monitor, Computer, Pc, Page, TV, Business Card

 

Creating Listeners

Let’s start with the application’s Developer Center.

Could contain: Page, Text

We’ve added a new tab for Listeners. This tab lists all your existing listeners, as well as the class on which it is listening.

You’ll notice that each listener can only observe a single class. While this may seem slightly tedious, the idea here was that as a developer, you should be conscious of what class you are working with. It also eliminates the need to check what type of object is being passed preventing accidents and unintended consequences when missing class checks.

When you add a listener, there are several listener types available. We will discuss each one in detail.

Could contain: Page, Text, Computer, Electronics, Pc, White Board

 

Content Listener

This listener is fired on an Item or Comment (or Review) class. When adding a Content Listener, you must specify the class you are observing. When you hit Save, there is a validation check in place to ensure that the class exists, and that it is a valid class for the selected listener type.

Content Listeners have the following methods available. All methods are included in the default listener file that is generated.

  • onLoad
    Triggered when an object is loaded (in Content::constructFromData)

NOTE: Be careful! This event could be fired at unexpected times. If you’re executing code here, you may want to check the dispatcher instance to verify that your code is running where you expect.

  • onBeforeCreateOrEdit
    Fired when a form is submitted, before it is saved. This is the equivalent of Item::processBeforeCreate and Item::processBeforeEdit. This method includes a parameter to indicate whether this is a create or edit.
  • onCreateOrEdit
    Fired after a form is saved. This is the equivalent of Item::processAfterCreate and Item::processAfterEdit. This method includes a parameter to indicate whether this is a create or edit.
  • onDelete
    Fired after an object is deleted.
  • onStatusChange
    Fired when any moderation action occurs. For example, when an item is pinned/unpinned, locked/unlocked. The moderation action is passed as a parameter so that your code can take that into account.
  • onMerge
    Triggered AFTER an item is merged.
  • onItemView
    Triggered when an item is viewed and the view count is incremented.

 

Invoice Listener

An Invoice listener is fired only on the \IPS\nexus\Invoice class. This listener does not require you to specify a class to extend.

The Invoice listener includes a single method, onStatusChange. This is fired when the invoice is saved and the status changes (pending/paid/expired/canceled).

 

Member Listener

The Member listener will be familiar to many of you. We have taken the MemberSync extension and moved it in its entirety to a listener, as it was a better fit for this structure. All the previous methods that were available are still available here. We have also added the following new methods:

  • onJoinClub
    Fired when a member joins a club. If approval is required, this is fired after they are approved.
  • onLeaveClub
    Fired when a member leaves or is removed from a club.
  • onEventRsvp
    Fired when a member responds to an event. The response is included as a parameter to this method.

 

Commerce Package Listener

The Commerce Package listener is fired on any implementation of \IPS\nexus\Invoice\Item. You must specify the class that you are observing.

The methods are the same as those that are available in an item extension.

  • onCancel
  • onChange
  • onDelete
  • onExpireWarning
  • onExpire
  • onInvoiceCancel
  • onPaid
  • onPurchaseGenerated
  • onReactivate
  • onRenew
  • onTransfer
  • onUnpaid

 

Some Technical Notes

  • All listener files will be generated in a “listeners” directory within your application.
  • Your application’s data directory will include a listeners.json file that defines all your listeners. The json file is automatically generated when you add/remove a listener.
  • All listener methods are optional. The default class that is generated will include all available methods, but they do not have to be present in your file.
  • All listener methods are return type void.
  • If a listener method fails, an exception will be thrown when IN_DEV is enabled. In production, the exception will be logged with a reference to the listener class and the event on which the exception occurred.

 

Firing Events

Your code can fire any existing event using the Event::fire method.

Example:

Event::fire( 'onBeforeCreateOrEdit', $comment, array( $values ) );

The Event::fire method is called statically and accepts an event name, the object on which the event will be fired, and an array of additional parameters (varies according to the event that you are triggering).

This concludes our introduction to Listeners. We do intend to implement additional listener types and events based on your feedback and as the need arises.

Comments

Recommended Comments



Miss_B

Posted

30 minutes ago, Matt said:

but if you do wish to move to xF then on the plus side you won't need to worry about new versions and there being significant changes in your future. 😄

I wish we could do multiple reactions here. I wanted to like, agree, meouw and haha this post all at once lol.

Very well said @Matt. Why people are saying that they want to go to a community platform that is as good as dead, with no communication from the management and with very bad support to match, is beyond me. That is the same as turning in your ferrari for a beat up car that won't take you a few km without giving you engline trouble.

 

AlexJ

Posted (edited)

1 hour ago, Matt said:

Already in v5.

This is great news...  u have no idea how much I have asked to Michael about adding subscriptions. It's mess right now for our users... their profiles don't reflect correct count because subscription is through Store and regular donation through Michael donation app as it provides yearly goals. For any donation based community, yearly donation goals are critical. 

1 hour ago, Matt said:

I think the sensible thing to do is wait for all the news of v5 to come out and then make an informed decision on your future. Trying to extrapolate v5 from a few dev blogs may lead to rash decisions.

OK we will await but the notes from 3rd party developers that their majority addons wouldn't work got me concerned because their app is what IPS makes good. I am already grumpy and will remain as such because service desk is going away and it's headache for me to integrate 3rd party apps with SSO.  I wish you can publish the article with some alternatives and migration steps, so folks like me can follow. Right now it's going to be trial and error. 

Thanks!

Edited by AlexJ
Esther E.

Posted

19 minutes ago, AlexJ said:

but the notes from 3rd party developers that their majority addons wouldn't work got me concerned because their app is what IPS makes good.

3rd party devs don't know that for sure yet. We haven't finished discussing the development tools, so they really can't tell what will and will not work. Most things are still doable. Some things are not. It's "wait and see" right now.

Adriano Faria

Posted

1 minute ago, Esther E. said:

Most things are still doable. Some things are not. It's "wait and see" right now.

Come on, it’s exactly the opposite. Some are possible. Most won’t be. 

Bookmarking this to use later. 👍

I will make a list of my resources that will not be possible to update, after initial tests on V5, of course.

Esther E.

Posted

1 minute ago, Adriano Faria said:

Come on, it’s exactly the opposite. Some are possible. Most won’t be. 

Bookmarking this to use later. 👍

When we designed the development toolkit (there were a few of us involved in these discussions), we looked at a very large sample size of modifications to see what we needed to accommodate. There were quite a few instances where a conversation started with "people shouldn't do X" and then one of us said "but what about in a scenario where ____?"

I have been doing a feature check on all my private clients (not a small number) to see what is and is not still doable, to see who will need a heads up that things won't work the same way in v5, etc. The majority will be fine.

The reality is, that when you take the full toolkit into account, with a little (not even a lot) of creativity, you can still do most things.

DawPi

Posted

24 minutes ago, Esther E. said:

we looked at a very large sample size of modifications

Publicly released. Are you aware of private, custom mods? 🙂

Esther E.

Posted

2 minutes ago, DawPi said:

Publicly released. Are you aware of private, custom mods? 🙂

My own, for starters. There is more than one other developer who builds modifications privately. IPS itself has private custom clients. So yes, we did take quite a large sample size. 

  • Management
Matt

Posted

16 minutes ago, DawPi said:

Publicly released. Are you aware of private, custom mods? 🙂

Esther is probably the most trusted name in that space, so we do indeed have that covered.

KT Walrus

Posted

5 minutes ago, Matt said:

Considered and decided against it faster than a quantum computer could have managed it.

Well then, please consider open sourcing v5. Many many of the most successful companies in our industry have an open source version of their products. Rather than shrinking revenue and profits, open source greatly accelerates sales and reduces future development and maintenance expenses as you can leverage the open source community and get millions of users and hundreds of thousands of paying customers. Also, it could lead to IPS Cloud being more reasonably priced, generating more sales and revenue.

  • Management
Matt

Posted

Just now, KT Walrus said:

Well then, please consider open sourcing v5. Many many of the most successful companies in our industry have an open source version of their products. Rather than shrinking revenue and profits, open source greatly accelerates sales and reduces future development and maintenance expenses as you can leverage the open source community and get millions of users and hundreds of thousands of paying customers. Also, it could lead to IPS Cloud being more reasonably priced, generating more sales and revenue.

Most of those successful companies have taken millions in VC funding so they can pay people until some point in the future they start making a profit.

KT Walrus

Posted

1 minute ago, Matt said:

Most of those successful companies have taken millions in VC funding so they can pay people until some point in the future they start making a profit.

You wouldn't need VC funding as you already have IPS Cloud business generating capital to invest in software development. Anyway, this is OT now, but it occurred to me that not having an open source version of your product is hindering your ability to grow to a large player in this industry.

And, I fear that you are about to lose even more third-party developers with your transition to v5 resulting in IC being a very niche product. Third-party developers are key to almost all successful growing internet companies.

  • Management
Matt

Posted

2 minutes ago, KT Walrus said:

You wouldn't need VC funding as you already have IPS Cloud business generating capital to invest in software development. Anyway, this is OT now, but it occurred to me that not having an open source version of your product is hindering your ability to grow to a large player in this industry.

And, I fear that you are about to lose even more third-party developers with your transition to v5 resulting in IC being a very niche product. Third-party developers are key to almost all successful growing internet companies.

Happily, this isn't the case, and marketplace sales have declined by 75% over the past few years showing that sales are not on the strength of after-market add-ons.

KT Walrus

Posted (edited)

23 minutes ago, Matt said:

Happily, this isn't the case, and marketplace sales have declined by 75% over the past few years showing that sales are not on the strength of after-market add-ons.

Existing sales are not, but I'm thinking about accelerating future sales of IPS Cloud which I believe would be greatly accelerated with an open-source self-hosted version of v4 or v5, especially if you could get the price of entry-level IPS Cloud to under $20/month. A free tier of the open-source version with limited features could make conversion to paying user accelerate.

Anyway, this is my last comment on this... I just wanted to get you to consider open source as an option to scale your business. You need all the help you can get from third-party developers to accelerate adoption of IPS Cloud, IMHO.

Edited by KT Walrus
  • Management
Charles

Posted

9 minutes ago, KT Walrus said:

Existing sales are not, but I'm thinking about accelerating future sales of IPS Cloud which I believe would be greatly accelerated with an open-source self-hosted version of v4 or v5, especially if you could get the price of entry-level IPS Cloud to under $20/month. A free tier of the open-source version with limited features could make conversion to paying user accelerate.

Anyway, this is my last comment on this... I just wanted to get you to consider open source as an option to scale your business. You need all the help you can get from third-party developers to accelerate adoption of IPS Cloud, IMHO.

Actually, cloud clients have a very low usage of Marketplace. People do on cloud do not tend to "tinker" quite as much and just get on with their community.

Esther E.

Posted

5 minutes ago, Adriano Faria said:

Were all the suggestions made (by me and others) implemented or?

Some.

We added the following methods to the Member listeners:

  • onReact
  • onUnreact
  • onFollow
  • onUnfollow

As mentioned in previous comments on this entry, we allowed for listeners on base classes, like \IPS\Content\Item.

I'm not sure what other suggestions you're referring to regarding listeners. Was there something specific?

Adriano Faria

Posted (edited)

20 minutes ago, Esther E. said:

we allowed for listeners on base classes, like \IPS\Content\Item.

Base classes (topic, file, etc) or global (\IPS\Content\Item). Kinda confusing it. We need to make it globally.

20 minutes ago, Esther E. said:

I'm not sure what other suggestions you're referring to regarding listeners. Was there something specific?

- Visit a profile:

When will be the "point"? It's missing only nodes in UI Extensions. As Member isn't a node, I'm wondering.

 

- Ignore/stop ignoring something (posts/messages/mentions).

So what is the outcome of the discussion?

Edited by Adriano Faria
Esther E.

Posted

17 minutes ago, Adriano Faria said:

- Visit a profile:

When will be the "point"? It's missing only nodes in UI Extensions. As Member isn't a node, I'm wondering.

I don't recall saying that UI Extensions had anything to do with this, and it was definitely not related to what I had in mind.

I also don't recall anyone saying that the last UI Extension entry would be the last dev tools entry. We still have a few more things coming that are not finalized yet.

17 minutes ago, Adriano Faria said:

- Ignore/stop ignoring something (posts/messages/mentions).

We discussed this, and decided not to implement it.

 

Not all requests are going to be fulfilled. We are happy to discuss them, but just because we talk about it doesn't mean the answer will be yes.

Adriano Faria

Posted (edited)

7 minutes ago, Esther E. said:

We are happy to discuss them, but just because we talk about it doesn't mean the answer will be yes.

Of course. What I'm trying to do is get some answers as we're left in the limbo.

Edited by Adriano Faria
Esther E.

Posted

1 minute ago, Adriano Faria said:

Of course. What I'm trying to do is get some answers as we're left in the limbo.

We're actually still adding a few things to the listeners, as there were some points that came up as we continue development. I do plan on posting a follow-up in the comments here when we're closer to finalizing those changes. Right now it looks like there may still be more, so I'd rather wait for a bit.

Adriano Faria

Posted (edited)

@Esther E.

Quote

Invoice Listener

An Invoice listener is fired only on the \IPS\nexus\Invoice class. This listener does not require you to specify a class to extend.

The Invoice listener includes a single method, onStatusChange. This is fired when the invoice is saved and the status changes (pending/paid/expired/canceled).

Just to confirm: will this return all data from the invoice? I mean, I need at least the i_items, to see if the invoice belongs to a purchase of a Downloads file.

Thank you.

Edited by Adriano Faria


×
×
  • Create New...