Skip to content

Instantly share code, notes, and snippets.

@BrightnBubbly
Created April 20, 2020 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrightnBubbly/ae8c6d7e4aae94490531075b743b010a to your computer and use it in GitHub Desktop.
Save BrightnBubbly/ae8c6d7e4aae94490531075b743b010a to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use GetStream\StreamChat\Client;
use Illuminate\Http\Request;
class ChatMessagesController extends Controller
{
protected $client;
public function __construct(){
$this->client = new Client(
getenv("MIX_STREAM_API_KEY"),
getenv("MIX_STREAM_API_SECRET"),
);
}
public function generateToken(Request $request){
try{
return response()->json([
'token' => $this->client->createToken($request->name)
], 200);
}catch(\Exception $e){
return response()->json([
'errorMessage' => $e->getMessage()
],500);
}
}
public function getChannel(Request $request){
try{
$from = $request->from;
$to = $request->to;
$from_username = $request->from_username;
$to_username = $request->to_username;
$channel_name = "livechat-{$from_username}-{$to_username}";
$channel = $this->client->getChannel("messaging", $channel_name);
$channel->create($from_username, [$to_username]);
return response()->json([
'channel' => $channel_name
], 200);
}catch(\Exception $e){
return response()->json([
'errorMessage' => $e->getMessage()
],500);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment