Skip to content

Instantly share code, notes, and snippets.

@dongido001
Created July 8, 2019 16:27
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 dongido001/9aaa0e05ea9ac4cd5c3c12935fe209e6 to your computer and use it in GitHub Desktop.
Save dongido001/9aaa0e05ea9ac4cd5c3c12935fe209e6 to your computer and use it in GitHub Desktop.
// [...]
/**
* Create or get channel
*/
public function createOrGetChannel(Request $request)
{
$from = $request->input('from');
$to = $request->input('to');
$from_username = $request->input('from_username');
$to_username = $request->input('to_username');
$channel = Channel::whereIn('from_id', [$from, $to])
->whereIn('to_id', [$from, $to])
->first();
// If channel if already created,return it else create it.
if ($channel) {
$channel_name = $channel->name;
} else {
$channel_name = "private-{$from_username}-{$to_username}";
$channel = new Channel();
$channel->name = $channel_name;
$channel->from_id = $from;
$channel->to_id = $to;
$channel->save();
// Create channel to Stream Chat
$channel = $this->client->getChannel("messaging", $channel_name);
$channel->create($from_username, [$to_username]);
}
return response()->json([
'channel' => $channel_name
], 200);
}
// [...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment