vocphone / laravel-matrix-sdk
PHP SDK for interacting with Matrix/Synapse.
Requires
- php: ~7.4|^8
- ext-json: *
- guzzlehttp/guzzle: ^6.3|^7
- laravel/helpers: ^1.7
Requires (Dev)
- phpunit/phpunit: >=5.4.3
Replaces
- aryess/php-matrix-sdk: 1.0.5
- aryess/phpmatrixsdk: 1.0.5
This package is auto-updated.
Last update: 2025-04-11 06:31:05 UTC
README
This package is a fork of the now-abandoned original by Yoann Celton. The architecture does not necessarily reflect best practice in the eyes of current maintainers. Incremental improvements are made as time and resources allow. Contributions are welcome.
This is a Matrix client-server SDK for php 7.4+, initially copied from matrix-org/matrix-python-sdk and then forked from meet-kinksters/php-matrix-sdk
This package is still a work in progress, and at the current time, not everything has been ported:
- Missing E2E encryption, need php bindings for the OLM library
- Live sync
- Unit tests for the client
At the time of writing this, most sdks are lacking in the ability to use spaces, this sdk is space aware.
Installation
composer require vocphone/laravel-matrix-sdk
Configuration
The most simple way to integrate is to add the following environment variables, the app will then cache the login token ( forever ) to assist with speed and perform less logins
MATRIX_URL=https://matrix.org
MATRIX_USERNAME=username
MATRIX_PASSWORD=password
Usage
Basic Usage
Send Message
use Vocphone\LaravelMatrixSdk\MatrixClient; $message = 'My Laravel Test Message'; $roomId = '!someRoom:matrix.org'; app(MatrixClient::class)->sendMessage($message, $roomId);
Notifications
There is the ability to send messages directly into matrix.
Notifiable
use Illuminate\Notifications\Notifiable; class User extends Model { use Notifiable; public function routeNotificationForMatrix($notification) { // If the roomId is null then the message won't attempt to send to matrix $roomId = $this->matrix_user_room_id ?? null; return $roomId; } }
Notification
create a notification that returns the message content from the toMatrix
method
use Illuminate\Notifications\Notification; class MatrixNotification extends Notification { private $message; public function __Construct( string $message ) { $this->message = $message; } public function toMatrix( $notifiable ) { return $this->message; } }
Spaces
Create a Space
use Vocphone\LaravelMatrixSdk\MatrixClient; $matrix = app(MatrixClient::class); $spaceName = 'My Cool Space'; $public = false; // No way man, it's just for me $invitees = []; // just me for now $isSpace = true; $space = $matrix->createRoom( $spaceName, $public, $invitees, $isSpace);
Add User to a space
use Vocphone\LaravelMatrixSdk\MatrixClient; $inviteUserId = '@new_user:matrix.org'; // get the matrix client instance $matrix = app(MatrixClient::class); // make sure we have all the current known information $matrix->sync(); foreach( $matrix->getRooms() as $room ) { if( $room->getIsSpace() ) { $room->inviteUser($inviteUserId); } }
Structure
The SDK is split into two modules: api
and client
.
API
This contains the raw HTTP API calls and has minimal business logic. You can
set the access token (token
) to use for requests as well as set a custom
transaction ID (txn_id
) which will be incremented for each request.
Client
This encapsulates the API module and provides object models such as Room
.
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email support@vocphone.com instead of using the issue tracker.
Credits
- Tom Higgins at Vocphone
- Brad Jones at Meet Kinksters
- Yoann Celton (initial port)
- All Contributors