How to use Neo4j with Server Side Swift (Vapor/Fluent/Theo)

I am interested in developing an application using the Neo4j graph database but I can’t find much documentation on how to leverage it with Vapor, Fluent, Theo etc… Does anyone no where I might find additional documentation?

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

@rob256 Hey, sorry for the delay, missed this one! So you should just be able to use the Swift driver directly from your Vapor route handlers.

So from the README, all the calls there can just live in your route handlers. Do be warned that it looks like it’s a blocking framework so you may encounter some issues if you want to run this on a high-traffic site.

In terms of integrating it with Fluent, that will require more work and I’m not aware of anyone who’s done it, so you might have to dig through source code if you want to do that!

Thank you for your assistance.

Hi Oxtim, I am new to server side in general and interested in Neo4j.
Can you please give more information on the blocking framework you are mentioning?
Is it a Vapor thing or Neo4j? Where did you spot it in the framework?
Please be as analytical as possible, you are my only source on this.
Thank you in advance.

@geoter I can see because the Theo framework doesn’t depend on SwiftNIO and has no mention of a Future type. So with something like FluentMySQL (Vapor’s MySQL wrapper), all the queries return EventLoopFuture<Model> or similar. This is because when you send the request to the database, you have to wait for the database to go and perform the query and return the results and send it back over the network.

In Theo (the Swift Neo4j framework) this waiting is done by just sitting there waiting for it to come back, meaning that thread can’t do anything else (similar to blocking the main UI thread on iOS) - which is what I mean by blocking. In Vapor 2, for instance, this is fine because it expects everything to be blocking so just spins up threads for everything incoming request. This can be very expensive however, so Vapor only has a handful of threads and will put down a waiting thread to go and handle some more work. If you block all of those threads waiting for results from Neo4j then you can’t do anything else, so it’s something you need to watch out for. A simple solution would be to wrap the calls in either your own promises or dispatch them onto a background thread

1 Like

You are the best. Thank you!

According to GitHub ( GitHub - Neo4j-Swift/Neo4j-Swift: Open Source Neo4j driver for iOS, tvOS, Linux and macOS ) Theo 4.0 uses the bolt framework which relies on SWIFTNIO. The examples shown on GitHub show both sync and async calls to the database. It appears that the async calls are the default.

Looking through the code it is asynchronous in that it uses completion handlers but will actually call wait() on the NIO futures, which blocks the thread and will crash when integrated with Vapor. The good news is that there’s an issue on GitHub tracking this with a branch to make it all work and people discussing using it with Vapor - I’d go from there as a way to integrate it!

This topic was automatically closed after 166 days. New replies are no longer allowed.