PHPackages                             phpfacile/chat-db - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. phpfacile/chat-db

ActiveLibrary[Database &amp; ORM](/categories/database)

phpfacile/chat-db
=================

Implementation of the phpfacile/chat interface (aimed to provide a chat/messenger service) with a database as storage

1.0.1(7y ago)018MITPHPPHP &gt;=7.0.0

Since Oct 22Pushed 7y agoCompare

[ Source](https://github.com/phpfacile/chat-db)[ Packagist](https://packagist.org/packages/phpfacile/chat-db)[ Docs](https://github.com/phpfacile/chat-db)[ RSS](/packages/phpfacile-chat-db/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (5)Versions (3)Used By (0)

PHPFacile! Chat-Db
==================

[](#phpfacile-chat-db)

This is an implementation of the phpfacile/chat interface using a database as a storage.

Installation
------------

[](#installation)

At the root of your project type

```
composer require phpfacile/chat-db

```

Or add "phpfacile/chat-db": "^1.0" to the "require" part of your composer.json file

```
"require": {
    "phpfacile/chat-db": "^1.0"
}

```

Your database must contain a table "chat\_messages" with (at least) the following fields:

- id: integer auto increment
- msg: string
- user\_id: integer or string
- channel\_id: integer or string
- insertion\_datetime\_utc: datetime or string

REM: In the current version table and field names are not configurable

Example of table creation query with SQLite (for test purpose only)

```
CREATE TABLE chat_messages (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    channel_id INTEGER UNSIGNED NOT NULL,
    user_id INTEGER UNSIGNED NOT NULL,
    msg TEXT NOT NULL,
    insertion_datetime_utc DATETIME NOT NULL
);

```

Example of table creation query with MySQL

```
CREATE TABLE `chat_messages` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `channel_id` BIGINT UNSIGNED NOT NULL,
  `user_id` BIGINT UNSIGNED NOT NULL,
  `msg` TEXT NOT NULL,
  `insertion_datetime_utc` DATETIME NOT NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

Usage
-----

[](#usage)

### Step 1 : Adapter instanciation

[](#step-1--adapter-instanciation)

Instanciate a Zend Adapter to allow a connexion to a database.

Example with SQLite (for test purpose only)

```
$config = [
    'driver' => 'Pdo_Sqlite',
    'database' => 'my_chat_database.sqlite',
];
$adapter = new Zend\Db\Adapter\Adapter($config);
```

Example with MySQL

```
$config = [
    'driver' => 'Pdo_Mysql',
    'host' => 'localhost'
    'dbname' => 'my_database',
    'user' => 'my_user',
    'password' => 'my_pass',
];
$adapter = new Zend\Db\Adapter\Adapter($config);
```

### Step 2 : ChatChannelService instanciation

[](#step-2--chatchannelservice-instanciation)

```
use PHPFacile\Chat\Service\ChatChannelService;

$chatChannelService = new ChatChannelService();
```

REM: You might have to overwrite the default ChatChannelService so as to control user accesses. (Cf. below)

### Step 3 : ChatService instanciation

[](#step-3--chatservice-instanciation)

```
use PHPFacile\Chat\Service\ChatService;

$chatService = new ChatService($adapter, $chatChannelService);
```

### Step 4 : Post or get messages or messages information

[](#step-4--post-or-get-messages-or-messages-information)

#### addMessage

[](#addmessage)

You can store add a new message to a chat channel by providing a text, a channel id and a user id to the **addMessage()** method.

```
$chatService->addMessage($text, $channelId, $userId);
```

REM: This method will not check whether the $channelId or the $userId (already) exists

#### getMessages

[](#getmessages)

You can retrieve all the messages (and its metadata) from a chat channel by providing a channel id and a user id to the **getMessages()** method.

```
$msgs = $chatService->getMessages($channelId, $userId);
```

This will return an array of StdClass containing both the text and associated data of the messages.

```
foreach ($msgs as $msg) {
    echo 'Id of the msg = '.$msg->id."\n";
    echo 'Text = '.$msg->text."\n";
    echo 'User id = '.$msg->user->id."\n";
    echo 'Posted at = '.$msg->insertionDateTimeUTC."\n";
}
```

#### getLastUserMessageDateTimeUTC

[](#getlastusermessagedatetimeutc)

You can also retrieve the date/time (in UTC) of the last message posted by a user in a chat channel by providing a channel id and a user id to the **getLastUserMessageDateTimeUTC()** method.

```
$dateTime = $chatService->getLastUserMessageDateTimeUTC($channelId, $userId);
```

This will return a date in string format (Y-m-d H:i:s) like '2018-12-25 22:30:10' or null if no message was posted.

### Advanced feature

[](#advanced-feature)

You're invited to overwrite the default ChatChannelService (or have you own ChatChannelServiceInterface implementation) so as to write your own user access rights management.

```
use PHPFacile\Chat\Service\ChatChannelServiceInterface;

class CustomChatChannelService implements ChatChannelServiceInterface
{
    public function isUserAllowedToAccessChannelMessages($userId, $channelId, $right)
    {
        // Return true if the user must be allowed to access
        // to the content of the chat channel either for
        // reading ($right = self::RIGHT_CHANNEL_MSG_READ) or for
        // writing ($right = self::RIGHT_CHANNEL_MSG_WRITE)
        return true;
    }
}

$chatChannelService = new CustomChatChannelService();
```

If ever you want you can store additionnal data with the posted message by providing an associative array as a 4th parameter of addMessage(). The array keys must match (existing) table field names.

```
$extraData = [
    'software' => 'MyChatApp',
];
$chatService->addMessage($text, $channelId, $userId, extraData);
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~85 days

Total

2

Last Release

2677d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9d6240c88e9505e69bf9ddcd371244851f480013df9c71c496c8d0fcd6de5357?d=identicon)[phpfacile](/maintainers/phpfacile)

---

Tags

chatMessenger

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/phpfacile-chat-db/health.svg)

```
[![Health](https://phpackages.com/badges/phpfacile-chat-db/health.svg)](https://phpackages.com/packages/phpfacile-chat-db)
```

###  Alternatives

[munafio/chatify

A package for Laravel PHP Framework to add a complete real-time chat system.

2.4k441.9k2](/packages/munafio-chatify)[lexxyungcarter/chatmessenger

Simple one-to-one/group chat messaging tool for Laravel 5, 6, 7, 8, 9 &amp; 10 with Pusher Integration

10724.1k](/packages/lexxyungcarter-chatmessenger)[syntaxlexx/chatmessenger

Simple one-to-one/group chat messaging tool for Laravel 5, 6, 7, 8, 9 &amp; 10 with Pusher Integration

10510.2k](/packages/syntaxlexx-chatmessenger)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
