承接 splitstack/translucid 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

splitstack/translucid

Composer 安装命令:

composer require splitstack/translucid

包简介

Realtime model updates for Laravel via Postgres LISTEN/NOTIFY or Eloquent lifecycle events

README 文档

README

Real-time model change broadcasting for Laravel — powered by PostgreSQL LISTEN/NOTIFY or Eloquent lifecycle events.

When a row changes in your database, Translucid fires a Laravel broadcast event on a private channel. Your frontend can subscribe and react instantly, without polling.

Requirements

  • PHP 8.2+
  • Laravel 11, 12, or 13
  • PostgreSQL (required for the DB-mode listener)
  • laravel/pennant ^1.0

Installation

composer require splitstack/translucid

Publish the config:

php artisan vendor:publish --tag=translucid-config

How it works

Translucid supports two broadcast sources — you pick one (or both) per scope via Pennant feature flags:

Mode How events are triggered
From DB (TranslucidFromDB) PostgreSQL triggers call pg_notify. A long-running translucid:listen command picks them up and dispatches broadcast events.
From App (TranslucidFromApp) The HasTranslucid trait adds Eloquent observers (created, updated, deleted) that dispatch broadcast events inline.

Both modes emit the same three events: TranslucidCreated, TranslucidUpdated, TranslucidDeleted.

Getting started

1. Install database triggers (DB mode)

Create a migration to register PostgreSQL triggers for each model you want to observe:

use App\Models\Post;
use Illuminate\Database\Migrations\Migration;
use Splitstack\Translucid\Facades\Translucid;

return new class extends Migration
{
    public function up(): void
    {
        Translucid::observe(Post::class);
    }

    public function down(): void
    {
        Translucid::unobserve(Post::class);
    }
};

Run migrations, then start the listener:

php artisan translucid:listen

2. Add the trait (App mode)

Add HasTranslucid to any Eloquent model to broadcast changes via Eloquent observers instead:

use Splitstack\Translucid\Concerns\HasTranslucid;

class Post extends Model
{
    use HasTranslucid;
}

3. Activate a feature flag

Both modes are gated by Pennant. Activate the flag for the scope you want (a user, tenant, or null for global):

use Laravel\Pennant\Feature;
use Splitstack\Translucid\Features\TranslucidFromDB;
use Splitstack\Translucid\Features\TranslucidFromApp;

// Activate DB mode globally
Feature::activate(TranslucidFromDB::class);

// Or activate App mode for a specific user
Feature::for($user)->activate(TranslucidFromApp::class);

Broadcast payload

Events broadcast on a private channel (default: translucid). Each event carries:

{
  "type": "posts",
  "model": "App\\Models\\Post",
  "id": "42",
  "op": "updated",
  "changes": { "title": "New title" }
}

The broadcast event name includes the table and record key:

translucid.updated.posts.42
translucid.created.posts.42
translucid.deleted.posts.42

Configuration

config/translucid.php:

return [
    // Base channel name (private channel). Multi-tenant mode appends ".{tenant}".
    'default_channel' => 'translucid',

    // Set a TenantDriver class for per-tenant channel scoping and per-tenant
    // PostgreSQL connections. null = single-tenant.
    'tenant_driver' => null,

    // Attribute on the Tenant model appended to the channel name.
    // Produces channels like "translucid.{tenant.space}".
    'tenant_channel_attribute' => 'space',

    // Laravel DB connection used for per-tenant PDO connections.
    'tenant_connection' => 'tenant',

    // Microseconds to sleep between LISTEN polling loops.
    'listen_sleep' => 50_000,
];

Multi-tenancy

Built-in Spatie driver

If you use spatie/laravel-multitenancy, configure the built-in driver:

// config/translucid.php
'tenant_driver' => \Splitstack\Translucid\Tenancy\SpatieMultitenancyDriver::class,
'tenant_channel_attribute' => 'space', // attribute on your Tenant model
'tenant_connection' => 'tenant',       // Laravel DB connection for tenant DBs

The listener will open a separate PostgreSQL LISTEN connection per tenant and broadcast on scoped channels like translucid.acme.

Custom driver

Implement the TenantDriver contract to integrate any tenancy strategy:

use PDO;
use Splitstack\Translucid\Contracts\TenantDriver;

class MyTenantDriver implements TenantDriver
{
    // Returns the broadcast channel for the currently active tenant.
    public function resolveChannel(): string
    {
        return 'translucid.' . MyTenancy::current()->slug;
    }

    // Returns a channel => PDO map. Each PDO must already have LISTEN issued.
    public function resolveListenConnections(): array
    {
        $connections = [];
        foreach (Tenant::all() as $tenant) {
            $pdo = new PDO(/* ... */);
            $pdo->exec('LISTEN translucid');
            $connections['translucid.' . $tenant->slug] = $pdo;
        }
        return $connections;
    }

    // Returns the Pennant scope string for the given tenant.
    public function resolveFeatureScope(mixed $scope): ?string
    {
        return 'translucid.' . $scope->slug;
    }
}

Register it in config/translucid.php:

'tenant_driver' => MyTenantDriver::class,

Custom channel resolver (without a driver)

For simple cases you can override the channel resolver directly in a service provider:

use Splitstack\Translucid\Translucid;

Translucid::resolveChannelUsing(fn () => 'my-app.' . auth()->id());

Artisan commands

Command Description
php artisan translucid:listen Start polling PostgreSQL for notifications and dispatching broadcast events. Handles SIGINT/SIGTERM for graceful shutdown.

Events reference

Event Broadcast name op value
TranslucidCreated translucid.created.{table}.{id} created
TranslucidUpdated translucid.updated.{table}.{id} updated
TranslucidDeleted translucid.deleted.{table}.{id} deleted

All three are standard Laravel broadcastable events and work with any broadcasting driver (Reverb, Pusher, Ably, etc.).

Testing

composer test

License

MIT — see LICENSE.md.

splitstack/translucid 适用场景与选型建议

splitstack/translucid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 310 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「postgres」 「WebSockets」 「laravel」 「notify」 「realtime」 「Broadcasting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 splitstack/translucid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 splitstack/translucid 我们能提供哪些服务?
定制开发 / 二次开发

基于 splitstack/translucid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 310
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-05