Skip to content

Clearfacts/stream-chat-php

 
 

Repository files navigation

Official PHP SDK for Stream Chat

build Latest Stable Version

Official PHP API client for Stream Chat, a service for building chat applications.
Explore the docs »

Report Bug · Request Feature

📝 About Stream

You can sign up for a Stream account at our Get Started page.

You can use this library to access chat API endpoints server-side.

For the client-side integrations (web and mobile) have a look at the JavaScript, iOS and Android SDK libraries (docs).

⚙️ Installation

$ composer require get-stream/stream-chat

✨ Getting started

require_once "./vendor/autoload.php";

Instantiate a new client, find your API keys in the dashboard.

$client = new GetStream\StreamChat\Client("<api-key>", "<api-secret>");

Generate a token for client-side usage

$token = $client->createToken("bob-1");

// with an expiration time
$expiration = (new DateTime())->getTimestamp() + 3600;
$token = $client->createToken("bob-1", $expiration);

Update / Create users

$bob = [
    'id' => 'bob-1',
    'role' => 'admin',
    'name' => 'Robert Tables',
];

$bob = $client->upsertUser($bob);

// Batch update is also supported
$jane = ['id' => 'jane', 'role' => 'admin'];
$june = ['id' => 'june', 'role' => 'user'];
$tom = ['id' => 'tom', 'role' => 'guest'];
$users = $client->upsertUsers([$jane, $june, $tom]);

Channel types

$channelConf = [
    'name' => 'livechat',
    'automod' => 'disabled',
    'commands' => ['ban'],
    'mutes' => true
];

$channelType = $client->createChannelType($channelConf);

$allChannelTypes =  $client->listChannelTypes();

Channels and messages

$channel = $client->Channel("messaging", "bob-and-jane");
$state = $channel->create("bob-1", ['bob-1', 'jane']);
$channel->addMembers(['mike', 'joe']);

Messaging

$msg_bob = $channel->sendMessage(["text" => "Hi June!"], 'bob-1');

// Reply to a message
$reply_bob = $channel->sendMessage(["text" => "Long time no see!"], 'bob-1', $msg_bob['message']['id']);

Reactions

$channel->sendReaction($reply_bob['message']['id'], ['type' => 'like'], 'june');

Moderation

$channel->addModerators(['june']);
$channel->demoteModerators(['june']);

$channel->banUser('june', ["reason" => "Being a big jerk", "timeout" => 5, "user_id" => 'bob-1']);
$channel->unbanUser('june', ["user_id" => 'bob-1']);

Devices

$device_id = "iOS_Device_Token_123";
$client->addDevice($device_id, "apn", "june");
$devices = $client->getDevices('june');

$client->deleteDevice($device_id, 'june');

🙋‍♀️ Frequently asked questions

  • Q: What date formats does the backend accept?
  • A: We accept RFC3339 format. So you either use raw strings as date or you implement a serializer for your DateTime object.
class MyDateTime extends \DateTime implements \JsonSerializable
{
    public function jsonSerialize()
    {
        // Note: this returns ISO8601
        // but it's compatible with 3339
       return $this->format("c");
    }
}

$createdAt = new MyDateTime();

$client->search( 
	['type' => "messaging"], 
	['created_at' => ['$lte' => $createdAt]], 
	['limit' => 10]
);

✍️ Contributing

We welcome code changes that improve this library or fix a problem, please make sure to follow all best practices and add tests if applicable before submitting a Pull Request on Github. We are very happy to merge your code in the official repository. Make sure to sign our Contributor License Agreement (CLA) first. See our license file for more details.

Head over to CONTRIBUTING.md for some development tips.

🧑‍💻 We are hiring!

We've recently closed a $38 million Series B funding round and we keep actively growing. Our APIs are used by more than a billion end-users, and you'll have a chance to make a huge impact on the product within a team of the strongest engineers all over the world.

Check out our current openings and apply via Stream's website.

Releases

No releases published

Packages

No packages published

Languages

  • PHP 98.9%
  • Other 1.1%