一对一聊天PHP

I am trying to develop a one to one chat. I want the users to only be able to see messages from the sender i.e the sender only sees the messages from himself and the receive but the query fails to run. I am able to see all the messages present in the database

public function getMessages() {

 $messages = array();
 $query = <<<QUERY
    SELECT 
      `chat`.`message`, 
      `chat`.`sent_on`,
      `user1`.`id`, 
      `user1`.`first_name`
    FROM `user1`
    JOIN `chat`
      ON `chat`.`user_id` = `user1`.`id` AND `chat`.`rec_id`=$recid
    ORDER BY `sent_on`
  QUERY;

You need to specify userId in your query like this:

public function getMessages($userId) {

  $messages = array();
  $query = <<<QUERY
     SELECT 
       `chat`.`message`, 
       `chat`.`sent_on`,
       `user1`.`id`, 
       `user1`.`first_name`
     FROM `user1`
     JOIN `chat`
       ON `chat`.`user_id` = `user1`.`id` AND `chat`.`rec_id`=$recid
     WHERE `user1`.`id` = {$userId}
     ORDER BY `sent_on`
   QUERY;