Efficient User Timelines in a PHP Application with Neo4j

Originally published at: http://www.sitepoint.com/efficient-user-timelines-php-application-neo4j/

Any social application application you encounter nowadays offers you a timeline, showing statuses of your friends or followers generally in a descending order of time.

Implementing such a feature has never been easy with common SQL or NoSQL databases.

Complexity of queries, performance impacts increasing with the number of friends/followers and difficulties to evolve your social model are points that graph databases are eliminating.

In this tutorial, we’re going to extend the demo application used by the two introduction articles about Neo4j and PHP, respectively :

The application is built on Silex and has users following other users. The goal throughout this article will be to model the feature of feeds efficiently in order to retrieve the last two feeds of the people you follow and order them by time.

You’ll discover a particular modeling technique called Linked list and some advanced queries with Cypher.

The source code for this article can be found in its own Github repository.

Modeling a timeline in a graph database

People who are used to other database modeling techniques tend to relate each feed to the user. A feed would have a timestamp property and the order of the feeds will be done against this property.

Here is a simple representation :

Continue reading this article on SitePoint

Nice Article. I love it when (relatively) new technologies are demonstrated with PHP, like this article demonstrates.

I’d just like to throw in this link to the Cypher library. https://github.com/endyjasmi/neo4j Maybe it might help someone find it quicker. I Googled for “Cypher PHP” and ended up on the Neo4J web site, but the link there to Github was broken.

I’d love to see more on how to work with PHP and Neo4J.

Does anyone else have any experience with Neo4J or with a REST API to a database? Is it performant? I would imagine it isn’t as fast as a native driver, or is it?


I moved 5 posts to an existing topic: Very strange 502 error still

1 Like

Thanks for your feedback.

I was aware of the library you mentioned, however by using extensively Neo4j in production at several customers I had never the need to implement this kind of Cypher wrapper.

The Rest API is really powerful, you can manage transactions like you would do with any native driver and it implements streaming. The main concern right now is that the API Response is really verbose so if you fetch 1000 nodes you have a really big json back while generally you will implement lazy loading at this stage.

There is some work on Neo4j internals for a new api protocol (SBE) which is far lot faster than JSON while JSON will still be supported.

Um, I was confused. You had shown code with a “sendCypherQuery” object and I went searching. The endyjasmi Cypher repo is the repo that Neo4J links to for a PHP driver on their website, just that their link is broken. It was the first Google result. At first glance, I thought it was the preferred driver, but now I see, that could be a misinterpretation of the page on my part. I also found this one. Is that the better “client” driver? Or which driver would you recommend?


The one you mentioned here : neoxygen/neo4j-neoclient is the one used in this tutorial. It is the most powerful, extensible and production-ready driver. I’m maintaining this driver and as I’m working in tight collaboration with the Neo4j team it has the last updates almost immediately.
Key features of this driver are :

  • Built-In Support for Neo4j Enterprise Edition (High-Availibitlity)
  • Multi-DB Support
  • A really powerful graph response formatter that remap the portion of the graph you fetch in php nodes, relationships and paths objects.

SendCypherQuery is used a lot, in fact Cypher is the SQL of Neo4j. There is btw at this time no real graph oriented object mapper, but we are working on it.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.