PHP code example of nahid / talk

1. Go to this page and download the library: Download nahid/talk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nahid / talk example snippets


Nahid\Talk\TalkServiceProvider::class,

'Talk'      => Nahid\Talk\Facades\Talk::class,

return [
    'user' => [
        'model' => 'App\User',
        'foreignKey' => null,
        'ownerKey' => null,
    ],

    'broadcast' => [
        'enable' => true,
        'app_name' => 'talk-example',
        'driver' => env('TALK_BROADCAST_DRIVER', 'pusher'), // pusher or laravel-websockets
        'pusher' => [
            'app_id' => env('PUSHER_APP_ID', ''),
            'app_key' => env('PUSHER_APP_KEY', ''),
            'app_secret' => env('PUSHER_APP_SECRET', ''),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER', 'ap2'),
                'encrypted' => env('PUSHER_APP_ENCRYPTION', false),
                'host' => '127.0.0.1',
                'port' => env('LARAVEL_WEBSOCKETS_PORT', 6001),
                'scheme' => 'http',
                'wsHost' => '127.0.0.1',
                'wsPort' => env('LARAVEL_WEBSOCKETS_PORT', 6001),
                'forceTLS' => false,
                'disableStats' => true
            ]
        ],
    ],


    'oembed' => [
        'enabled' => false,
        'url' => '',
        'key' => ''
    ]
];

 'talk'  =>  \Nahid\Talk\Middleware\TalkMiddleware::class,
 

 $this->middleware('talk');
 

Talk::setAuthUserId(auth()->user()->id);

Talk::user(auth()->user()->id)->anyMethodHere();

void setAuthUserId($userid)

function __construct()
{
    Talk::setAuthUserId(auth()->user()->id);
}

object user($id)

$inboxes = Talk::user(auth()->user()->id)->threads();
return view('messages.threads', compact('inboxes'));

int|false isConversationExists($userid)

if ($conversationId = Talk::isConversationExists($userId)) {
    Talk::sendMessage($conversationId, $message);
} 

boolean isAuthenticUser($conversationId, $userId)

if (Talk::isAuthenticUser($conversationId, $userId)) {
    Talk::sendMessage($conversationId, $message);
} 

object|false sendMessage($conversationId, $message)

    $message = Talk::sendMessage($conversationId, $message);
    if ($message) {
        return response()->json(['status'=>'success', 'data'=>$message], 200);
   }

object|false sendMessageByUserId($userId, $message)

array getInbox([$order = 'desc'[,$offset = 0[, $take = 20]]])

// controller method
$inboxes = Talk::getInbox();
return view('message.threads', compact('inboxes'));

object getInboxAll([$order = 'desc'[,$offset = 0[, $take = 20]]])

array threads([$order = 'desc'[,$offset = 0[, $take = 20]]])

array threadsAll([$order = 'desc'[,$offset = 0[, $take = 20]]])

array getConversationsById($conversationId[, $offset = 0[, $take = 20]])

// controller method
$conversations = Talk::getConversationsById($conversationId);
$messages = $conversations->messages;
$withUser = $conversations->withUser;

return view('messages.conversations', compact('messages', 'withUser'));

array getConversationsAllById($conversationId[, $offset = 0[, $take = 20]])

object getConversationsByUserId($receiverId [, $offset = 0[, $take = 20]])

array getConversationsAllByUserId($receiverId[, $offset = 0[, $take = 20]])

array messages($conversationId[, $offset = 0[, $take = 20]])

array messagesAll($conversationId[, $offset = 0[, $take = 20]])

array messagesByUserId($receiverId[, $offset = 0[, $take = 20]])

array messagesAllByUserId($receiverId[, $offset = 0[, $take = 20]])

array readMessage($messageId)

object getReceiverInfo($conversationId)

boolean makeSeen($messageId)

boolean deleteMessage($messageId)

boolean deleteForever($messageId)

boolean deleteConversations($conversationId)

return [
    'user' => [
        'model' => 'App\User',
        'foreignKey' => null,
        'ownerKey' => null,
    ],

    'broadcast' => [
        'enable' => true,
        'app_name' => 'talk-example',
        'driver' => env('TALK_BROADCAST_DRIVER', 'pusher'), // pusher or laravel-websockets
        'pusher' => [
            'app_id' => env('PUSHER_APP_ID', ''),
            'app_key' => env('PUSHER_APP_KEY', ''),
            'app_secret' => env('PUSHER_APP_SECRET', ''),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER', 'ap2'),
                'encrypted' => env('PUSHER_APP_ENCRYPTION', false),
                'host' => '127.0.0.1',
                'port' => env('LARAVEL_WEBSOCKETS_PORT', 6001),
                'scheme' => 'http',
                'wsHost' => '127.0.0.1',
                'wsPort' => env('LARAVEL_WEBSOCKETS_PORT', 6001),
                'forceTLS' => false,
                'disableStats' => true
            ]
        ],
    ],


    'oembed' => [
        'enabled' => false,
        'url' => '',
        'key' => ''
    ]
];

    'user' => [
        ...
    ],
    ...
    ],
    'oembed' => [
        'enabled' => true,
        'url' => 'http://your.domain/api/oembed',
        'key' => 'yout-auth-api-key'
    ]

php artisan vendor:publish --provider="Nahid\Talk\TalkServiceProvider"
shell
php artisan migrate
html
<!-- messages/threads.blade.php -->
<ul>
    @foreach($inboxes as $inbox)
        <li>
            <h2>{{$inbox->withUser->name}}</h2>
            <p>{{$inbox->thread->message}}</p>
            <span>{{$inbox->thread->humans_time}}</span>
        </li>
    @endforeach
</ul>
html
<!-- messages/conversations.blade.php -->
<div class="message-container">
    <h2>Chat with {{$withUser->name}}</h2>
    @foreach ($messages as $msg)
     <div class="message">
        <h4>{{$msg->sender->name}}</h4>
        <span>{{$msg->humans_time}}</span>
        <p>
            {{$msg->message}}
       </p> 
    </div>
    @endforeach
</div>
html
<div class="message-container">
    <h2>Chat with {{$withUser->name}}</h2>
    @foreach ($messages as $msg)
     <div class="message">
        <h4>{{$msg->sender->name}}</h4>
        <span>{{$msg->humans_time}}</span>
        <p>
            {{$msg->toHtmlString()}}
       </p> 
    </div>
    @endforeach
</div>