public function generate(Request $request, AccessToken $accessToken, IpMessagingGrant $ipmGrant)
 {
     $appName = "TwilioChat";
     $deviceId = $request->input("device");
     $identity = $request->input("identity");
     $TWILIO_IPM_SERVICE_SID = config('services.twilio')['ipmServiceSid'];
     $endpointId = $appName . ":" . $identity . ":" . $deviceId;
     $accessToken->setIdentity($identity);
     $ipmGrant->setServiceSid($TWILIO_IPM_SERVICE_SID);
     $ipmGrant->setEndpointId($endpointId);
     $accessToken->addGrant($ipmGrant);
     $response = array('identity' => $identity, 'token' => $accessToken->toJWT());
     return response()->json($response);
 }
<?php

// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php';
// Loads the library
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\IpMessagingGrant;
// Required for all Twilio access tokens
$twilioAccountSid = 'ACxxxxxxxxxxxx';
$twilioApiKey = 'SKxxxxxxxxxxxx';
$twilioApiSecret = 'xxxxxxxxxxxxxx';
// Required for IP messaging grant
$ipmServiceSid = 'ISxxxxxxxxxxxx';
// An identifier for your app - can be anything you'd like
$appName = 'TwilioChatDemo';
// choose a random username for the connecting user
$identity = "john_doe";
// A device ID should be passed as a query string parameter to this script
$deviceId = 'somedevice';
$endpointId = $appName . ':' . $identity . ':' . $deviceId;
// Create access token, which we will serialize and send to the client
$token = new AccessToken($twilioAccountSid, $twilioApiKey, $twilioApiSecret, 3600, $identity);
// Create IP Messaging grant
$ipmGrant = new IpMessagingGrant();
$ipmGrant->setServiceSid($ipmServiceSid);
$ipmGrant->setEndpointId($endpointId);
// Add grant to token
$token->addGrant($ipmGrant);
// render token to string
echo $token->toJWT();
<?php

// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php';
// Loads the library
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\ConversationsGrant;
// Required for all Twilio access tokens
$twilioAccountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$twilioApiKey = 'SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$twilioApiSecret = 'your_api_secret';
$TwilioConfigurationSid = 'VSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// choose a random username for the connecting user
$identity = 'randomUsername';
// Create access token, which we will serialize and send to the client
$token = new AccessToken($TWILIO_ACCOUNT_SID, $TWILIO_API_KEY, $TWILIO_API_SECRET, 3600, $identity);
// Grant access to Video
$grant = new VideoGrant();
$grant->setConfigurationProfileSid($TwilioConfigurationSid);
$token->addGrant($grant);
// return serialized token and the user's randomly generated ID
echo json_encode(array('identity' => $identity, 'token' => $token->toJWT()));