DEV Community

Cover image for ThingsDB and PHP
Michal Štefaňák
Michal Štefaňák

Posted on

ThingsDB and PHP

Maybe you never heard about ThingsDB. Me neither until last month. First thing should be some introduction of ThingsDB. At their website you can read:

ThingsDB logo

ThingsDB is an open-source Stored-State-Distributed-Interpreter (SSDI) solution written in the C programming language. It is designed to provide a powerful and scalable platform for storing, managing, and processing data. ThingsDB is particularly well-suited for applications that require real-time data processing, high availability, and fault tolerance.

I'm not going to lie, it seemed intruiging to me and I started thinking about what project I can build with it. But I saw immediately there are only few available drivers for communication besides standard HTTP API. I always prefer using socket connection because it's faster than making HTTP requests. It was the same case like when I decided to build client for communication with Neo4j (Bolt).

Fortunately they provide well made documentation and that made my job easier. It took few days and driver was working very well. I've learned a bit about ThingsDB while doing that, which gives me more knowledge for building my project around it. My driver is available at github as opensource: thingsdb-php.


Easiest way is to install it into your project is with composer. Driver is made with simple approach. You just create new instance of class ThingsDB. It will immediately connect to provided uri and afterwards you can start communicating with ThingsDB. In the beginning there is just few methods needed:

  • auth - most important one, you need to authorize
  • authToken - same as auth but this one is for token authorization
  • query - sending query to database
  • run - run a procedure available in database

The simplest example of using my driver looks like this:

use ThingsDB\ThingsDB;
$thingsDB = new ThingsDB();
$result = $thingsDB->auth();
// on successful login $result will be true
$message = $thingsDB->query('@:stuff', '"Hello World!";'); 
// $message will contain "Hello World!" 
Enter fullscreen mode Exit fullscreen mode

auth accepts arguments for username and password but ThingsDB default "admin" and "pass" is set as default value. Sending simple "Hello World!" is send back to us. But you can do way more.

There is a lot of stuff you can do directly in ThingsDB. Go ahead and check their documentation.

Let me show you another example using run method for executing procedures:

use ThingsDB\ThingsDB;
$thingsDB = new ThingsDB();
$result = $thingsDB->auth();

// check existing procedure, destroy it if it exists
$exists = $thingsDB->query('@:stuff', 'has_procedure(procName);', ['procName' => 'multiply']);
if ($exists) {
    $thingsDB->query('@:stuff', 'del_procedure(procName);', ['procName' => 'multiply']);
}
// create procedure
$thingsDB->query('@:stuff', 'new_procedure(procName, |x, y| x * y);', ['procName' => 'multiply']);

// call procedure
$result = $thingsDB->run('@:stuff', 'multiply', [4, 5]);
// $result will be int(20)
Enter fullscreen mode Exit fullscreen mode

For simplicity of example I'm not checking every query response and also I didn't surround the logic with try catch.

If you want to see more examples, you can head to tests directory of repository.


Are you asking what more the driver can do? ThingsDB supports rooms which you can join, leave and most importantly, you can listen for emit. This way different actions can be invoked across all connected clients. Here is a set of available methods for this:

  • join - join room(s)
  • leave - leave room(s)
  • emit - emit an event in room
  • listening - Listen for incoming events

listening should be used with caution. It will pause php execution and just waits for communication coming from database. You can decide how long you want to wait for incoming message. If you run php script with max_execution_time=0, you can even wait indefinitely.


I hope you like it and will give it a try. If you do and you would like to support me, you can give a GitHub star to my repository at thingsdb-php or you can click on donate button. Even so you if you wish to support ThingsDB you can do the same at their repository at ThingsDB.

ko-fi

I'm also working on javascript driver (link) but there is still few bumps on the road. Hopefuly it will be resolved soon.

Top comments (0)