├── .gitattributes ├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── README.md ├── app ├── LaravelRealtimeChat │ └── Repositories │ │ ├── Conversation │ │ ├── ConversationRepository.php │ │ └── DbConversationRepository.php │ │ ├── DbRepository.php │ │ ├── Message │ │ ├── DbMessageRepository.php │ │ └── MessageRepository.php │ │ └── User │ │ ├── DbUserRepository.php │ │ └── UserRepository.php ├── commands │ └── .gitkeep ├── config │ ├── app.php │ ├── auth.php │ ├── cache.php │ ├── compile.php │ ├── database.php │ ├── local │ │ ├── .gitignore │ │ └── app.php │ ├── mail.php │ ├── packages │ │ └── .gitkeep │ ├── queue.php │ ├── remote.php │ ├── services.php │ ├── session.php │ ├── testing │ │ ├── cache.php │ │ └── session.php │ ├── view.php │ └── workbench.php ├── controllers │ ├── .gitkeep │ ├── AuthController.php │ ├── BaseController.php │ ├── ChatController.php │ ├── ConversationController.php │ ├── ConversationUserController.php │ ├── HomeController.php │ └── MessageController.php ├── database │ ├── .gitignore │ ├── migrations │ │ ├── .gitkeep │ │ ├── 2014_11_18_015623_create_users_table.php │ │ ├── 2014_11_18_020041_create_conversations_table.php │ │ ├── 2014_11_18_020407_create_messages_table.php │ │ ├── 2014_11_18_023111_create_conversations_users_table.php │ │ └── 2014_11_19_193700_create_messages_notifications.php │ └── seeds │ │ ├── .gitkeep │ │ ├── ConversationsTableSeeder.php │ │ ├── ConversationsUsersTableSeeder.php │ │ ├── DatabaseSeeder.php │ │ ├── MessagesNotificationsTableSeeder.php │ │ ├── MessagesTableSeeder.php │ │ └── UsersTableSeeder.php ├── events │ ├── ChatConversationsEventHandler.php │ └── ChatMessagesEventHandler.php ├── filters.php ├── lang │ └── en │ │ ├── pagination.php │ │ ├── reminders.php │ │ └── validation.php ├── listeners.php ├── models │ ├── Conversation.php │ ├── ConversationUser.php │ ├── Message.php │ ├── MessageNotification.php │ └── User.php ├── routes.php ├── start │ ├── artisan.php │ ├── global.php │ └── local.php ├── storage │ ├── .gitignore │ ├── cache │ │ └── .gitignore │ ├── logs │ │ └── .gitignore │ ├── meta │ │ └── .gitignore │ ├── sessions │ │ └── .gitignore │ └── views │ │ └── .gitignore ├── tests │ ├── ExampleTest.php │ └── TestCase.php └── views │ ├── emails │ └── auth │ │ └── reminder.blade.php │ ├── layouts │ └── main.blade.php │ └── templates │ ├── chat.blade.php │ ├── conversations.blade.php │ ├── login.blade.php │ ├── messages.blade.php │ └── new_message_modal.blade.php ├── artisan ├── bootstrap ├── autoload.php ├── paths.php └── start.php ├── composer.json ├── dump.rdb ├── nodejs ├── index.html ├── package.json └── server.js ├── npm-debug.log ├── phpunit.xml ├── public ├── .htaccess ├── css │ └── chat.css ├── favicon.ico ├── img │ ├── gusfring.jpg │ ├── hank.jpg │ ├── heisenberg.jpg │ ├── pinkman.jpg │ └── skyler.jpg ├── index.php ├── js │ └── chat.js ├── packages │ └── .gitkeep └── robots.txt ├── realtime.sh └── server.php /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /bootstrap/compiled.php 2 | /vendor 3 | composer.phar 4 | composer.lock 5 | .env.*.php 6 | .env.php 7 | .DS_Store 8 | Thumbs.db 9 | /nodejs/node_modules 10 | /app/config/database.php 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | sudo: false 4 | 5 | php: 6 | - 5.4 7 | - 5.5 8 | - 5.6 9 | - 7.0 10 | 11 | matrix: 12 | allow_failures: 13 | - php: 7.0 14 | 15 | install: travis_retry composer install --no-interaction --prefer-source 16 | 17 | script: vendor/bin/phpunit 18 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribution Guidelines 2 | 3 | Please submit all issues and pull requests to the [laravel/framework](http://github.com/laravel/framework) repository! 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Realtime Chat 2 | [![Build Status](https://travis-ci.org/guilhermeslk/laravel-realtime-chat.svg?branch=master)](https://travis-ci.org/guilhermeslk/laravel-realtime-chat) 3 | [![Code Climate](https://codeclimate.com/github/guilhermeslk/laravel-realtime-chat/badges/gpa.svg)](https://codeclimate.com/github/guilhermeslk/laravel-realtime-chat) 4 | 5 | A realtime chat sample written in Laravel 4.2 + Redis + Node.js + Socket.io. 6 | 7 | Um exemplo de chat realtime escrito em Laravel 4.2 + Redis Node.js + Socket.io (instruções somente em inglês). 8 | 9 | Live example available at: http://chat.guilhermeslk.com.br :) 10 | 11 | ## Requirements 12 | Laravel 4.2 13 | MySQL 14 | Redis 15 | Node.js 16 | NPM 17 | 18 | ## How to install 19 | ### Step 1: Clone this repo 20 | 21 | ```bash 22 | $ git clone https://github.com/guilhermeslk/laravel-realtime-chat.git 23 | ``` 24 | ### Step 2: Install composer packages 25 | 26 | ```bash 27 | $ cd laravel-realtime-chat 28 | $ composer install 29 | ``` 30 | ### Step 3: Configure Database 31 | Edit your ***database.php*** to match your local database settings. 32 | 33 | ```php 34 | ... 35 | 'connections' => array( 36 | 37 | 'mysql' => array( 38 | 'driver' => 'mysql', 39 | 'host' => '', 40 | 'database' => '', 41 | 'username' => '', 42 | 'password' => '', 43 | 'charset' => 'utf8', 44 | 'collation' => 'utf8_unicode_ci', 45 | 'prefix' => '', 46 | ), 47 | ... 48 | ``` 49 | ### Step 4: Migrate & Populate Database 50 | Run these commands to create and populate your database: 51 | 52 | ```bash 53 | $ php artisan migrate 54 | $ php artisan db:seed 55 | ``` 56 | 57 | ### Step 5: Install NPM dependencies 58 | CD into the nodejs folder and run npm install in order to have the all dependencies installed. 59 | 60 | ```bash 61 | $ cd nodejs 62 | $ npm install 63 | ``` 64 | 65 | ### Step 6: Run PHP's built-in development server 66 | 67 | ```bash 68 | $ cd .. 69 | $ php artisan serve 70 | ``` 71 | ### Step 7: Start Redis Server 72 | 73 | ```bash 74 | $ redis-server 75 | ``` 76 | 77 | ### Step 8: Start the "Realtime" server (nodejs/server.js) 78 | 79 | ```bash 80 | $ ./realtime.sh 81 | ``` 82 | 83 | That's it! Now you should be ready to go! 84 | -------------------------------------------------------------------------------- /app/LaravelRealtimeChat/Repositories/Conversation/ConversationRepository.php: -------------------------------------------------------------------------------- 1 | model = $model; 15 | } 16 | 17 | public function getByName($name) 18 | { 19 | return $this->model->where('name', $name)->firstOrFail(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/LaravelRealtimeChat/Repositories/DbRepository.php: -------------------------------------------------------------------------------- 1 | model = $model; 10 | } 11 | 12 | public function getById($id) 13 | { 14 | return $this->model->find($id); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/LaravelRealtimeChat/Repositories/Message/DbMessageRepository.php: -------------------------------------------------------------------------------- 1 | model = $model; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/LaravelRealtimeChat/Repositories/Message/MessageRepository.php: -------------------------------------------------------------------------------- 1 | model = $model; 15 | } 16 | 17 | public function getAllExcept($id) 18 | { 19 | return $this->model->where('id', '<>', $id)->get(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/LaravelRealtimeChat/Repositories/User/UserRepository.php: -------------------------------------------------------------------------------- 1 | false, 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Application URL 21 | |-------------------------------------------------------------------------- 22 | | 23 | | This URL is used by the console to properly generate URLs when using 24 | | the Artisan command line tool. You should set this to the root of 25 | | your application so that it is used when running Artisan tasks. 26 | | 27 | */ 28 | 29 | 'url' => 'http://localhost', 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Application Timezone 34 | |-------------------------------------------------------------------------- 35 | | 36 | | Here you may specify the default timezone for your application, which 37 | | will be used by the PHP date and date-time functions. We have gone 38 | | ahead and set this to a sensible default for you out of the box. 39 | | 40 | */ 41 | 42 | 'timezone' => 'UTC', 43 | 44 | /* 45 | |-------------------------------------------------------------------------- 46 | | Application Locale Configuration 47 | |-------------------------------------------------------------------------- 48 | | 49 | | The application locale determines the default locale that will be used 50 | | by the translation service provider. You are free to set this value 51 | | to any of the locales which will be supported by the application. 52 | | 53 | */ 54 | 55 | 'locale' => 'en', 56 | 57 | /* 58 | |-------------------------------------------------------------------------- 59 | | Application Fallback Locale 60 | |-------------------------------------------------------------------------- 61 | | 62 | | The fallback locale determines the locale to use when the current one 63 | | is not available. You may change the value to correspond to any of 64 | | the language folders that are provided through your application. 65 | | 66 | */ 67 | 68 | 'fallback_locale' => 'en', 69 | 70 | /* 71 | |-------------------------------------------------------------------------- 72 | | Encryption Key 73 | |-------------------------------------------------------------------------- 74 | | 75 | | This key is used by the Illuminate encrypter service and should be set 76 | | to a random, 32 character string, otherwise these encrypted strings 77 | | will not be safe. Please do this before deploying an application! 78 | | 79 | */ 80 | 81 | 'key' => 'YourSecretKey!!!', 82 | 83 | 'cipher' => MCRYPT_RIJNDAEL_128, 84 | 85 | /* 86 | |-------------------------------------------------------------------------- 87 | | Autoloaded Service Providers 88 | |-------------------------------------------------------------------------- 89 | | 90 | | The service providers listed here will be automatically loaded on the 91 | | request to your application. Feel free to add your own services to 92 | | this array to grant expanded functionality to your applications. 93 | | 94 | */ 95 | 96 | 'providers' => array( 97 | 98 | 'Illuminate\Foundation\Providers\ArtisanServiceProvider', 99 | 'Illuminate\Auth\AuthServiceProvider', 100 | 'Illuminate\Cache\CacheServiceProvider', 101 | 'Illuminate\Session\CommandsServiceProvider', 102 | 'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider', 103 | 'Illuminate\Routing\ControllerServiceProvider', 104 | 'Illuminate\Cookie\CookieServiceProvider', 105 | 'Illuminate\Database\DatabaseServiceProvider', 106 | 'Illuminate\Encryption\EncryptionServiceProvider', 107 | 'Illuminate\Filesystem\FilesystemServiceProvider', 108 | 'Illuminate\Hashing\HashServiceProvider', 109 | 'Illuminate\Html\HtmlServiceProvider', 110 | 'Illuminate\Log\LogServiceProvider', 111 | 'Illuminate\Mail\MailServiceProvider', 112 | 'Illuminate\Database\MigrationServiceProvider', 113 | 'Illuminate\Pagination\PaginationServiceProvider', 114 | 'Illuminate\Queue\QueueServiceProvider', 115 | 'Illuminate\Redis\RedisServiceProvider', 116 | 'Illuminate\Remote\RemoteServiceProvider', 117 | 'Illuminate\Auth\Reminders\ReminderServiceProvider', 118 | 'Illuminate\Database\SeedServiceProvider', 119 | 'Illuminate\Session\SessionServiceProvider', 120 | 'Illuminate\Translation\TranslationServiceProvider', 121 | 'Illuminate\Validation\ValidationServiceProvider', 122 | 'Illuminate\View\ViewServiceProvider', 123 | 'Illuminate\Workbench\WorkbenchServiceProvider', 124 | 125 | ), 126 | 127 | /* 128 | |-------------------------------------------------------------------------- 129 | | Service Provider Manifest 130 | |-------------------------------------------------------------------------- 131 | | 132 | | The service provider manifest is used by Laravel to lazy load service 133 | | providers which are not needed for each request, as well to keep a 134 | | list of all of the services. Here, you may set its storage spot. 135 | | 136 | */ 137 | 138 | 'manifest' => storage_path().'/meta', 139 | 140 | /* 141 | |-------------------------------------------------------------------------- 142 | | Class Aliases 143 | |-------------------------------------------------------------------------- 144 | | 145 | | This array of class aliases will be registered when this application 146 | | is started. However, feel free to register as many as you wish as 147 | | the aliases are "lazy" loaded so they don't hinder performance. 148 | | 149 | */ 150 | 151 | 'aliases' => array( 152 | 153 | 'App' => 'Illuminate\Support\Facades\App', 154 | 'Artisan' => 'Illuminate\Support\Facades\Artisan', 155 | 'Auth' => 'Illuminate\Support\Facades\Auth', 156 | 'Blade' => 'Illuminate\Support\Facades\Blade', 157 | 'Cache' => 'Illuminate\Support\Facades\Cache', 158 | 'ClassLoader' => 'Illuminate\Support\ClassLoader', 159 | 'Config' => 'Illuminate\Support\Facades\Config', 160 | 'Controller' => 'Illuminate\Routing\Controller', 161 | 'Cookie' => 'Illuminate\Support\Facades\Cookie', 162 | 'Crypt' => 'Illuminate\Support\Facades\Crypt', 163 | 'DB' => 'Illuminate\Support\Facades\DB', 164 | 'Eloquent' => 'Illuminate\Database\Eloquent\Model', 165 | 'Event' => 'Illuminate\Support\Facades\Event', 166 | 'File' => 'Illuminate\Support\Facades\File', 167 | 'Form' => 'Illuminate\Support\Facades\Form', 168 | 'Hash' => 'Illuminate\Support\Facades\Hash', 169 | 'HTML' => 'Illuminate\Support\Facades\HTML', 170 | 'Input' => 'Illuminate\Support\Facades\Input', 171 | 'Lang' => 'Illuminate\Support\Facades\Lang', 172 | 'Log' => 'Illuminate\Support\Facades\Log', 173 | 'Mail' => 'Illuminate\Support\Facades\Mail', 174 | 'Paginator' => 'Illuminate\Support\Facades\Paginator', 175 | 'Password' => 'Illuminate\Support\Facades\Password', 176 | 'Queue' => 'Illuminate\Support\Facades\Queue', 177 | 'Redirect' => 'Illuminate\Support\Facades\Redirect', 178 | 'Redis' => 'Illuminate\Support\Facades\Redis', 179 | 'Request' => 'Illuminate\Support\Facades\Request', 180 | 'Response' => 'Illuminate\Support\Facades\Response', 181 | 'Route' => 'Illuminate\Support\Facades\Route', 182 | 'Schema' => 'Illuminate\Support\Facades\Schema', 183 | 'Seeder' => 'Illuminate\Database\Seeder', 184 | 'Session' => 'Illuminate\Support\Facades\Session', 185 | 'SoftDeletingTrait' => 'Illuminate\Database\Eloquent\SoftDeletingTrait', 186 | 'SSH' => 'Illuminate\Support\Facades\SSH', 187 | 'Str' => 'Illuminate\Support\Str', 188 | 'URL' => 'Illuminate\Support\Facades\URL', 189 | 'Validator' => 'Illuminate\Support\Facades\Validator', 190 | 'View' => 'Illuminate\Support\Facades\View', 191 | 192 | ), 193 | 194 | ); 195 | -------------------------------------------------------------------------------- /app/config/auth.php: -------------------------------------------------------------------------------- 1 | 'eloquent', 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Authentication Model 23 | |-------------------------------------------------------------------------- 24 | | 25 | | When using the "Eloquent" authentication driver, we need to know which 26 | | Eloquent model should be used to retrieve your users. Of course, it 27 | | is often just the "User" model but you may use whatever you like. 28 | | 29 | */ 30 | 31 | 'model' => 'User', 32 | 33 | /* 34 | |-------------------------------------------------------------------------- 35 | | Authentication Table 36 | |-------------------------------------------------------------------------- 37 | | 38 | | When using the "Database" authentication driver, we need to know which 39 | | table should be used to retrieve your users. We have chosen a basic 40 | | default value but you may easily change it to any table you like. 41 | | 42 | */ 43 | 44 | 'table' => 'users', 45 | 46 | /* 47 | |-------------------------------------------------------------------------- 48 | | Password Reminder Settings 49 | |-------------------------------------------------------------------------- 50 | | 51 | | Here you may set the settings for password reminders, including a view 52 | | that should be used as your password reminder e-mail. You will also 53 | | be able to set the name of the table that holds the reset tokens. 54 | | 55 | | The "expire" time is the number of minutes that the reminder should be 56 | | considered valid. This security feature keeps tokens short-lived so 57 | | they have less time to be guessed. You may change this as needed. 58 | | 59 | */ 60 | 61 | 'reminder' => array( 62 | 63 | 'email' => 'emails.auth.reminder', 64 | 65 | 'table' => 'password_reminders', 66 | 67 | 'expire' => 60, 68 | 69 | ), 70 | 71 | ); 72 | -------------------------------------------------------------------------------- /app/config/cache.php: -------------------------------------------------------------------------------- 1 | 'file', 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | File Cache Location 23 | |-------------------------------------------------------------------------- 24 | | 25 | | When using the "file" cache driver, we need a location where the cache 26 | | files may be stored. A sensible default has been specified, but you 27 | | are free to change it to any other place on disk that you desire. 28 | | 29 | */ 30 | 31 | 'path' => storage_path().'/cache', 32 | 33 | /* 34 | |-------------------------------------------------------------------------- 35 | | Database Cache Connection 36 | |-------------------------------------------------------------------------- 37 | | 38 | | When using the "database" cache driver you may specify the connection 39 | | that should be used to store the cached items. When this option is 40 | | null the default database connection will be utilized for cache. 41 | | 42 | */ 43 | 44 | 'connection' => null, 45 | 46 | /* 47 | |-------------------------------------------------------------------------- 48 | | Database Cache Table 49 | |-------------------------------------------------------------------------- 50 | | 51 | | When using the "database" cache driver we need to know the table that 52 | | should be used to store the cached items. A default table name has 53 | | been provided but you're free to change it however you deem fit. 54 | | 55 | */ 56 | 57 | 'table' => 'cache', 58 | 59 | /* 60 | |-------------------------------------------------------------------------- 61 | | Memcached Servers 62 | |-------------------------------------------------------------------------- 63 | | 64 | | Now you may specify an array of your Memcached servers that should be 65 | | used when utilizing the Memcached cache driver. All of the servers 66 | | should contain a value for "host", "port", and "weight" options. 67 | | 68 | */ 69 | 70 | 'memcached' => array( 71 | 72 | array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100), 73 | 74 | ), 75 | 76 | /* 77 | |-------------------------------------------------------------------------- 78 | | Cache Key Prefix 79 | |-------------------------------------------------------------------------- 80 | | 81 | | When utilizing a RAM based store such as APC or Memcached, there might 82 | | be other applications utilizing the same cache. So, we'll specify a 83 | | value to get prefixed to all our keys so we can avoid collisions. 84 | | 85 | */ 86 | 87 | 'prefix' => 'laravel', 88 | 89 | ); 90 | -------------------------------------------------------------------------------- /app/config/compile.php: -------------------------------------------------------------------------------- 1 | PDO::FETCH_CLASS, 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Default Database Connection Name 21 | |-------------------------------------------------------------------------- 22 | | 23 | | Here you may specify which of the database connections below you wish 24 | | to use as your default connection for all database work. Of course 25 | | you may use many connections at once using the Database library. 26 | | 27 | */ 28 | 29 | 'default' => 'mysql', 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Database Connections 34 | |-------------------------------------------------------------------------- 35 | | 36 | | Here are each of the database connections setup for your application. 37 | | Of course, examples of configuring each database platform that is 38 | | supported by Laravel is shown below to make development simple. 39 | | 40 | | 41 | | All database work in Laravel is done through the PHP PDO facilities 42 | | so make sure you have the driver for your particular database of 43 | | choice installed on your machine before you begin development. 44 | | 45 | */ 46 | 47 | 'connections' => array( 48 | 49 | 'sqlite' => array( 50 | 'driver' => 'sqlite', 51 | 'database' => __DIR__.'/../database/production.sqlite', 52 | 'prefix' => '', 53 | ), 54 | 55 | 'mysql' => array( 56 | 'driver' => 'mysql', 57 | 'host' => 'localhost', 58 | 'database' => 'forge', 59 | 'username' => 'forge', 60 | 'password' => '', 61 | 'charset' => 'utf8', 62 | 'collation' => 'utf8_unicode_ci', 63 | 'prefix' => '', 64 | ), 65 | 66 | 'pgsql' => array( 67 | 'driver' => 'pgsql', 68 | 'host' => 'localhost', 69 | 'database' => 'forge', 70 | 'username' => 'forge', 71 | 'password' => '', 72 | 'charset' => 'utf8', 73 | 'prefix' => '', 74 | 'schema' => 'public', 75 | ), 76 | 77 | 'sqlsrv' => array( 78 | 'driver' => 'sqlsrv', 79 | 'host' => 'localhost', 80 | 'database' => 'database', 81 | 'username' => 'root', 82 | 'password' => '', 83 | 'prefix' => '', 84 | ), 85 | 86 | ), 87 | 88 | /* 89 | |-------------------------------------------------------------------------- 90 | | Migration Repository Table 91 | |-------------------------------------------------------------------------- 92 | | 93 | | This table keeps track of all the migrations that have already run for 94 | | your application. Using this information, we can determine which of 95 | | the migrations on disk haven't actually been run in the database. 96 | | 97 | */ 98 | 99 | 'migrations' => 'migrations', 100 | 101 | /* 102 | |-------------------------------------------------------------------------- 103 | | Redis Databases 104 | |-------------------------------------------------------------------------- 105 | | 106 | | Redis is an open source, fast, and advanced key-value store that also 107 | | provides a richer set of commands than a typical key-value systems 108 | | such as APC or Memcached. Laravel makes it easy to dig right in. 109 | | 110 | */ 111 | 112 | 'redis' => array( 113 | 114 | 'cluster' => false, 115 | 116 | 'default' => array( 117 | 'host' => '127.0.0.1', 118 | 'port' => 6379, 119 | 'database' => 0, 120 | ), 121 | 122 | ), 123 | 124 | ); 125 | -------------------------------------------------------------------------------- /app/config/local/.gitignore: -------------------------------------------------------------------------------- 1 | *.php -------------------------------------------------------------------------------- /app/config/local/app.php: -------------------------------------------------------------------------------- 1 | true, 17 | 18 | ); 19 | -------------------------------------------------------------------------------- /app/config/mail.php: -------------------------------------------------------------------------------- 1 | 'smtp', 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | SMTP Host Address 23 | |-------------------------------------------------------------------------- 24 | | 25 | | Here you may provide the host address of the SMTP server used by your 26 | | applications. A default option is provided that is compatible with 27 | | the Mailgun mail service which will provide reliable deliveries. 28 | | 29 | */ 30 | 31 | 'host' => 'smtp.mailgun.org', 32 | 33 | /* 34 | |-------------------------------------------------------------------------- 35 | | SMTP Host Port 36 | |-------------------------------------------------------------------------- 37 | | 38 | | This is the SMTP port used by your application to deliver e-mails to 39 | | users of the application. Like the host we have set this value to 40 | | stay compatible with the Mailgun e-mail application by default. 41 | | 42 | */ 43 | 44 | 'port' => 587, 45 | 46 | /* 47 | |-------------------------------------------------------------------------- 48 | | Global "From" Address 49 | |-------------------------------------------------------------------------- 50 | | 51 | | You may wish for all e-mails sent by your application to be sent from 52 | | the same address. Here, you may specify a name and address that is 53 | | used globally for all e-mails that are sent by your application. 54 | | 55 | */ 56 | 57 | 'from' => array('address' => null, 'name' => null), 58 | 59 | /* 60 | |-------------------------------------------------------------------------- 61 | | E-Mail Encryption Protocol 62 | |-------------------------------------------------------------------------- 63 | | 64 | | Here you may specify the encryption protocol that should be used when 65 | | the application send e-mail messages. A sensible default using the 66 | | transport layer security protocol should provide great security. 67 | | 68 | */ 69 | 70 | 'encryption' => 'tls', 71 | 72 | /* 73 | |-------------------------------------------------------------------------- 74 | | SMTP Server Username 75 | |-------------------------------------------------------------------------- 76 | | 77 | | If your SMTP server requires a username for authentication, you should 78 | | set it here. This will get used to authenticate with your server on 79 | | connection. You may also set the "password" value below this one. 80 | | 81 | */ 82 | 83 | 'username' => null, 84 | 85 | /* 86 | |-------------------------------------------------------------------------- 87 | | SMTP Server Password 88 | |-------------------------------------------------------------------------- 89 | | 90 | | Here you may set the password required by your SMTP server to send out 91 | | messages from your application. This will be given to the server on 92 | | connection so that the application will be able to send messages. 93 | | 94 | */ 95 | 96 | 'password' => null, 97 | 98 | /* 99 | |-------------------------------------------------------------------------- 100 | | Sendmail System Path 101 | |-------------------------------------------------------------------------- 102 | | 103 | | When using the "sendmail" driver to send e-mails, we will need to know 104 | | the path to where Sendmail lives on this server. A default path has 105 | | been provided here, which will work well on most of your systems. 106 | | 107 | */ 108 | 109 | 'sendmail' => '/usr/sbin/sendmail -bs', 110 | 111 | /* 112 | |-------------------------------------------------------------------------- 113 | | Mail "Pretend" 114 | |-------------------------------------------------------------------------- 115 | | 116 | | When this option is enabled, e-mail will not actually be sent over the 117 | | web and will instead be written to your application's logs files so 118 | | you may inspect the message. This is great for local development. 119 | | 120 | */ 121 | 122 | 'pretend' => false, 123 | 124 | ); 125 | -------------------------------------------------------------------------------- /app/config/packages/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guilhermeslk/laravel-realtime-chat/0da3a88e6e5d0f71774f43ea4206e576f45d51a1/app/config/packages/.gitkeep -------------------------------------------------------------------------------- /app/config/queue.php: -------------------------------------------------------------------------------- 1 | 'sync', 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Queue Connections 23 | |-------------------------------------------------------------------------- 24 | | 25 | | Here you may configure the connection information for each server that 26 | | is used by your application. A default configuration has been added 27 | | for each back-end shipped with Laravel. You are free to add more. 28 | | 29 | */ 30 | 31 | 'connections' => array( 32 | 33 | 'sync' => array( 34 | 'driver' => 'sync', 35 | ), 36 | 37 | 'beanstalkd' => array( 38 | 'driver' => 'beanstalkd', 39 | 'host' => 'localhost', 40 | 'queue' => 'default', 41 | 'ttr' => 60, 42 | ), 43 | 44 | 'sqs' => array( 45 | 'driver' => 'sqs', 46 | 'key' => 'your-public-key', 47 | 'secret' => 'your-secret-key', 48 | 'queue' => 'your-queue-url', 49 | 'region' => 'us-east-1', 50 | ), 51 | 52 | 'iron' => array( 53 | 'driver' => 'iron', 54 | 'host' => 'mq-aws-us-east-1.iron.io', 55 | 'token' => 'your-token', 56 | 'project' => 'your-project-id', 57 | 'queue' => 'your-queue-name', 58 | 'encrypt' => true, 59 | ), 60 | 61 | 'redis' => array( 62 | 'driver' => 'redis', 63 | 'queue' => 'default', 64 | ), 65 | 66 | ), 67 | 68 | /* 69 | |-------------------------------------------------------------------------- 70 | | Failed Queue Jobs 71 | |-------------------------------------------------------------------------- 72 | | 73 | | These options configure the behavior of failed queue job logging so you 74 | | can control which database and table are used to store the jobs that 75 | | have failed. You may change them to any database / table you wish. 76 | | 77 | */ 78 | 79 | 'failed' => array( 80 | 81 | 'database' => 'mysql', 'table' => 'failed_jobs', 82 | 83 | ), 84 | 85 | ); 86 | -------------------------------------------------------------------------------- /app/config/remote.php: -------------------------------------------------------------------------------- 1 | 'production', 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Remote Server Connections 21 | |-------------------------------------------------------------------------- 22 | | 23 | | These are the servers that will be accessible via the SSH task runner 24 | | facilities of Laravel. This feature radically simplifies executing 25 | | tasks on your servers, such as deploying out these applications. 26 | | 27 | */ 28 | 29 | 'connections' => array( 30 | 31 | 'production' => array( 32 | 'host' => '', 33 | 'username' => '', 34 | 'password' => '', 35 | 'key' => '', 36 | 'keyphrase' => '', 37 | 'root' => '/var/www', 38 | ), 39 | 40 | ), 41 | 42 | /* 43 | |-------------------------------------------------------------------------- 44 | | Remote Server Groups 45 | |-------------------------------------------------------------------------- 46 | | 47 | | Here you may list connections under a single group name, which allows 48 | | you to easily access all of the servers at once using a short name 49 | | that is extremely easy to remember, such as "web" or "database". 50 | | 51 | */ 52 | 53 | 'groups' => array( 54 | 55 | 'web' => array('production') 56 | 57 | ), 58 | 59 | ); 60 | -------------------------------------------------------------------------------- /app/config/services.php: -------------------------------------------------------------------------------- 1 | array( 18 | 'domain' => '', 19 | 'secret' => '', 20 | ), 21 | 22 | 'mandrill' => array( 23 | 'secret' => '', 24 | ), 25 | 26 | 'stripe' => array( 27 | 'model' => 'User', 28 | 'secret' => '', 29 | ), 30 | 31 | ); 32 | -------------------------------------------------------------------------------- /app/config/session.php: -------------------------------------------------------------------------------- 1 | 'file', 20 | 21 | /* 22 | |-------------------------------------------------------------------------- 23 | | Session Lifetime 24 | |-------------------------------------------------------------------------- 25 | | 26 | | Here you may specify the number of minutes that you wish the session 27 | | to be allowed to remain idle before it expires. If you want them 28 | | to immediately expire on the browser closing, set that option. 29 | | 30 | */ 31 | 32 | 'lifetime' => 120, 33 | 34 | 'expire_on_close' => false, 35 | 36 | /* 37 | |-------------------------------------------------------------------------- 38 | | Session File Location 39 | |-------------------------------------------------------------------------- 40 | | 41 | | When using the native session driver, we need a location where session 42 | | files may be stored. A default has been set for you but a different 43 | | location may be specified. This is only needed for file sessions. 44 | | 45 | */ 46 | 47 | 'files' => storage_path().'/sessions', 48 | 49 | /* 50 | |-------------------------------------------------------------------------- 51 | | Session Database Connection 52 | |-------------------------------------------------------------------------- 53 | | 54 | | When using the "database" or "redis" session drivers, you may specify a 55 | | connection that should be used to manage these sessions. This should 56 | | correspond to a connection in your database configuration options. 57 | | 58 | */ 59 | 60 | 'connection' => null, 61 | 62 | /* 63 | |-------------------------------------------------------------------------- 64 | | Session Database Table 65 | |-------------------------------------------------------------------------- 66 | | 67 | | When using the "database" session driver, you may specify the table we 68 | | should use to manage the sessions. Of course, a sensible default is 69 | | provided for you; however, you are free to change this as needed. 70 | | 71 | */ 72 | 73 | 'table' => 'sessions', 74 | 75 | /* 76 | |-------------------------------------------------------------------------- 77 | | Session Sweeping Lottery 78 | |-------------------------------------------------------------------------- 79 | | 80 | | Some session drivers must manually sweep their storage location to get 81 | | rid of old sessions from storage. Here are the chances that it will 82 | | happen on a given request. By default, the odds are 2 out of 100. 83 | | 84 | */ 85 | 86 | 'lottery' => array(2, 100), 87 | 88 | /* 89 | |-------------------------------------------------------------------------- 90 | | Session Cookie Name 91 | |-------------------------------------------------------------------------- 92 | | 93 | | Here you may change the name of the cookie used to identify a session 94 | | instance by ID. The name specified here will get used every time a 95 | | new session cookie is created by the framework for every driver. 96 | | 97 | */ 98 | 99 | 'cookie' => 'laravel_session', 100 | 101 | /* 102 | |-------------------------------------------------------------------------- 103 | | Session Cookie Path 104 | |-------------------------------------------------------------------------- 105 | | 106 | | The session cookie path determines the path for which the cookie will 107 | | be regarded as available. Typically, this will be the root path of 108 | | your application but you are free to change this when necessary. 109 | | 110 | */ 111 | 112 | 'path' => '/', 113 | 114 | /* 115 | |-------------------------------------------------------------------------- 116 | | Session Cookie Domain 117 | |-------------------------------------------------------------------------- 118 | | 119 | | Here you may change the domain of the cookie used to identify a session 120 | | in your application. This will determine which domains the cookie is 121 | | available to in your application. A sensible default has been set. 122 | | 123 | */ 124 | 125 | 'domain' => null, 126 | 127 | /* 128 | |-------------------------------------------------------------------------- 129 | | HTTPS Only Cookies 130 | |-------------------------------------------------------------------------- 131 | | 132 | | By setting this option to true, session cookies will only be sent back 133 | | to the server if the browser has a HTTPS connection. This will keep 134 | | the cookie from being sent to you if it can not be done securely. 135 | | 136 | */ 137 | 138 | 'secure' => false, 139 | 140 | ); 141 | -------------------------------------------------------------------------------- /app/config/testing/cache.php: -------------------------------------------------------------------------------- 1 | 'array', 19 | 20 | ); 21 | -------------------------------------------------------------------------------- /app/config/testing/session.php: -------------------------------------------------------------------------------- 1 | 'array', 20 | 21 | ); 22 | -------------------------------------------------------------------------------- /app/config/view.php: -------------------------------------------------------------------------------- 1 | array(__DIR__.'/../views'), 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Pagination View 21 | |-------------------------------------------------------------------------- 22 | | 23 | | This view will be used to render the pagination link output, and can 24 | | be easily customized here to show any view you like. A clean view 25 | | compatible with Twitter's Bootstrap is given to you by default. 26 | | 27 | */ 28 | 29 | 'pagination' => 'pagination::slider-3', 30 | 31 | ); 32 | -------------------------------------------------------------------------------- /app/config/workbench.php: -------------------------------------------------------------------------------- 1 | '', 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Workbench Author E-Mail Address 21 | |-------------------------------------------------------------------------- 22 | | 23 | | Like the option above, your e-mail address is used when generating new 24 | | workbench packages. The e-mail is placed in your composer.json file 25 | | automatically after the package is created by the workbench tool. 26 | | 27 | */ 28 | 29 | 'email' => '', 30 | 31 | ); 32 | -------------------------------------------------------------------------------- /app/controllers/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guilhermeslk/laravel-realtime-chat/0da3a88e6e5d0f71774f43ea4206e576f45d51a1/app/controllers/.gitkeep -------------------------------------------------------------------------------- /app/controllers/AuthController.php: -------------------------------------------------------------------------------- 1 | 'required|email', 21 | 'password' => 'required|min:6' 22 | ); 23 | 24 | $validator = Validator::make($input, $rules); 25 | 26 | if($validator->fails()) { 27 | return Redirect::route('auth.postLogin')->withErrors($validator); 28 | } 29 | 30 | if(Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password')), false)) { 31 | return Redirect::route('chat.index'); 32 | } 33 | } 34 | 35 | public function logout() { 36 | Auth::logout(); 37 | return Redirect::to('/'); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /app/controllers/BaseController.php: -------------------------------------------------------------------------------- 1 | layout)) 13 | { 14 | $this->layout = View::make($this->layout); 15 | } 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /app/controllers/ChatController.php: -------------------------------------------------------------------------------- 1 | conversationRepository = $conversationRepository; 21 | $this->userRepository = $userRepository; 22 | } 23 | 24 | /** 25 | * Display the chat index. 26 | * 27 | * @return Response 28 | */ 29 | public function index() { 30 | 31 | $viewData = array(); 32 | 33 | if(Input::has('conversation')) { 34 | $viewData['current_conversation'] = $this->conversationRepository->getByName(Input::get('conversation')); 35 | } else { 36 | $viewData['current_conversation'] = Auth::user()->conversations()->first(); 37 | } 38 | 39 | if($viewData['current_conversation']) { 40 | Session::set('current_conversation', $viewData['current_conversation']->name); 41 | 42 | foreach($viewData['current_conversation']->messages_notifications as $notification) { 43 | $notification->read = true; 44 | $notification->save(); 45 | } 46 | } 47 | 48 | $users = $this->userRepository->getAllExcept(Auth::user()->id); 49 | 50 | foreach($users as $key => $user) { 51 | $viewData['recipients'][$user->id] = $user->username; 52 | } 53 | 54 | $viewData['conversations'] = Auth::user()->conversations()->get(); 55 | 56 | return View::make('templates/chat', $viewData); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /app/controllers/ConversationController.php: -------------------------------------------------------------------------------- 1 | conversationRepository = $conversationRepository; 20 | $this->userRepository = $userRepository; 21 | } 22 | 23 | /** 24 | * Display a listing of conversations. 25 | * 26 | * @return Response 27 | */ 28 | public function index() 29 | { 30 | $viewData = array(); 31 | 32 | $users = $this->userRepository->getAllExcept(Auth::user()->id); 33 | 34 | foreach($users as $key => $user) { 35 | $viewData['recipients'][$user->id] = $user->username; 36 | } 37 | 38 | $viewData['current_conversation'] = $this->conversationRepository->getByName(Input::get('conversation')); 39 | $viewData['conversations'] = Auth::user()->conversations()->get(); 40 | 41 | return View::make('templates/conversations', $viewData); 42 | } 43 | 44 | /** 45 | * Store a newly created conversation in storage. 46 | * 47 | * @return Response 48 | */ 49 | public function store() 50 | { 51 | 52 | $rules = array( 53 | 'users' => 'required|array', 54 | 'body' => 'required' 55 | ); 56 | 57 | $validator = Validator::make(Input::only('users', 'body'), $rules); 58 | 59 | if($validator->fails()) { 60 | return Response::json([ 61 | 'success' => false, 62 | 'result' => $validator->messages() 63 | ]); 64 | } 65 | 66 | // Create Conversation 67 | $params = array( 68 | 'created_at' => new DateTime, 69 | 'name' => str_random(30), 70 | 'author_id' => Auth::user()->id 71 | ); 72 | 73 | $conversation = Conversation::create($params); 74 | 75 | $conversation->users()->attach(Input::get('users')); 76 | $conversation->users()->attach(array(Auth::user()->id)); 77 | 78 | // Create Message 79 | $params = array( 80 | 'conversation_id' => $conversation->id, 81 | 'body' => Input::get('body'), 82 | 'user_id' => Auth::user()->id, 83 | 'created_at' => new DateTime 84 | ); 85 | 86 | $message = Message::create($params); 87 | 88 | // Create Message Notifications 89 | $messages_notifications = array(); 90 | 91 | foreach(Input::get('users') as $user_id) { 92 | array_push($messages_notifications, new MessageNotification(array('user_id' => $user_id, 'read' => false, 'conversation_id' => $conversation->id))); 93 | 94 | // Publish Data To Redis 95 | $data = array( 96 | 'room' => $user_id, 97 | 'message' => array('conversation_id' => $conversation->id) 98 | ); 99 | 100 | Event::fire(ChatConversationsEventHandler::EVENT, array(json_encode($data))); 101 | } 102 | 103 | $message->messages_notifications()->saveMany($messages_notifications); 104 | 105 | return Redirect::route('chat.index', array('conversation', $conversation->name)); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /app/controllers/ConversationUserController.php: -------------------------------------------------------------------------------- 1 | lists('conversation_id'); 12 | 13 | $conversations = array(); 14 | 15 | if($conversations_users) { 16 | $conversations = Conversation::whereIn('id', $conversations_users)->get(); 17 | } 18 | 19 | return Response::json([ 20 | 'success' => true, 21 | 'result' => $conversations 22 | ]); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/controllers/HomeController.php: -------------------------------------------------------------------------------- 1 | first(); 13 | $messages = Message::where('conversation_id', $conversation->id)->orderBy('created_at')->get(); 14 | 15 | return View::make('templates/messages')->with('messages', $messages)->render(); 16 | } 17 | 18 | /** 19 | * Store a newly created message in storage. 20 | * 21 | * @return Response 22 | */ 23 | public function store() { 24 | 25 | $rules = array('body' => 'required'); 26 | $validator = Validator::make(Input::all(), $rules); 27 | 28 | if($validator->fails()) { 29 | return Response::json([ 30 | 'success' => false, 31 | 'result' => $validator->messages() 32 | ]); 33 | } 34 | 35 | $conversation = Conversation::where('name', Input::get('conversation'))->first(); 36 | 37 | $params = array( 38 | 'conversation_id' => $conversation->id, 39 | 'body' => Input::get('body'), 40 | 'user_id' => Input::get('user_id'), 41 | 'created_at' => new DateTime 42 | ); 43 | 44 | $message = Message::create($params); 45 | 46 | // Create Message Notifications 47 | $messages_notifications = array(); 48 | 49 | foreach($conversation->users()->get() as $user) { 50 | array_push($messages_notifications, new MessageNotification(array('user_id' => $user->id, 'conversation_id' => $conversation->id, 'read' => false))); 51 | } 52 | 53 | $message->messages_notifications()->saveMany($messages_notifications); 54 | 55 | // Publish Data To Redis 56 | $data = array( 57 | 'room' => Input::get('conversation'), 58 | 'message' => array( 'body' => Str::words($message->body, 5), 'user_id' => Input::get('user_id')) 59 | ); 60 | 61 | Event::fire(ChatMessagesEventHandler::EVENT, array(json_encode($data))); 62 | 63 | return Response::json([ 64 | 'success' => true, 65 | 'result' => $message 66 | ]); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /app/database/.gitignore: -------------------------------------------------------------------------------- 1 | *.sqlite 2 | -------------------------------------------------------------------------------- /app/database/migrations/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guilhermeslk/laravel-realtime-chat/0da3a88e6e5d0f71774f43ea4206e576f45d51a1/app/database/migrations/.gitkeep -------------------------------------------------------------------------------- /app/database/migrations/2014_11_18_015623_create_users_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->string('email')->unique(); 18 | $table->string('username')->unique(); 19 | $table->string('password'); 20 | $table->string('image_path'); 21 | $table->rememberToken(); 22 | $table->timestamps(); 23 | }); 24 | } 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::drop('users'); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /app/database/migrations/2014_11_18_020041_create_conversations_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->dateTime('created_at'); 18 | $table->string('name'); 19 | $table->integer('author_id')->unsigned(); 20 | $table->foreign('author_id')->references('id')->on('users'); 21 | }); 22 | } 23 | 24 | /** 25 | * Reverse the migrations. 26 | * 27 | * @return void 28 | */ 29 | public function down() 30 | { 31 | Schema::drop('conversations'); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /app/database/migrations/2014_11_18_020407_create_messages_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->dateTime('created_at'); 18 | $table->text('body'); 19 | $table->integer('conversation_id')->unsigned(); 20 | $table->foreign('conversation_id')->references('id')->on('conversations'); 21 | $table->integer('user_id')->unsigned(); 22 | $table->foreign('user_id')->references('id')->on('users'); 23 | }); 24 | } 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::drop('messages'); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /app/database/migrations/2014_11_18_023111_create_conversations_users_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->integer('user_id')->unsigned(); 18 | $table->foreign('user_id')->references('id')->on('users'); 19 | $table->integer('conversation_id')->unsigned(); 20 | $table->foreign('conversation_id')->references('id')->on('users'); 21 | }); 22 | } 23 | 24 | /** 25 | * Reverse the migrations. 26 | * 27 | * @return void 28 | */ 29 | public function down() 30 | { 31 | Schema::drop('conversations_users'); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /app/database/migrations/2014_11_19_193700_create_messages_notifications.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->integer('user_id')->unsigned(); 18 | $table->foreign('user_id')->references('id')->on('users'); 19 | $table->integer('message_id')->unsigned(); 20 | $table->foreign('message_id')->references('id')->on('messages'); 21 | $table->integer('conversation_id')->unsigned(); 22 | $table->foreign('conversation_id')->references('id')->on('conversations'); 23 | $table->boolean('read'); 24 | $table->timestamps(); 25 | }); 26 | } 27 | 28 | /** 29 | * Reverse the migrations. 30 | * 31 | * @return void 32 | */ 33 | public function down() 34 | { 35 | Schema::drop('messages_notifications'); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /app/database/seeds/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guilhermeslk/laravel-realtime-chat/0da3a88e6e5d0f71774f43ea4206e576f45d51a1/app/database/seeds/.gitkeep -------------------------------------------------------------------------------- /app/database/seeds/ConversationsTableSeeder.php: -------------------------------------------------------------------------------- 1 | delete(); 8 | 9 | $user1 = DB::table('users')->where('username', 'heisenberg')->first(); 10 | $user2 = DB::table('users')->where('username', 'skyler')->first(); 11 | 12 | $conversations = array( 13 | array( 14 | 'created_at' => new DateTime, 15 | 'name' => str_random(30), 16 | 'author_id' => $user1->id 17 | ), 18 | array( 19 | 'created_at' => new DateTime, 20 | 'name' => str_random(30), 21 | 'author_id' => $user2->id 22 | ) 23 | ); 24 | 25 | DB::table('conversations')->insert($conversations); 26 | } 27 | } -------------------------------------------------------------------------------- /app/database/seeds/ConversationsUsersTableSeeder.php: -------------------------------------------------------------------------------- 1 | delete(); 8 | 9 | $user1 = DB::table('users')->where('username', 'heisenberg')->first(); 10 | $user2 = DB::table('users')->where('username', 'pinkman')->first(); 11 | $user3 = DB::table('users')->where('username', 'skyler')->first(); 12 | $user4 = DB::table('users')->where('username', 'hank')->first(); 13 | 14 | $conversation1 = DB::table('conversations')->where('author_id', $user1->id)->first(); 15 | $conversation2 = DB::table('conversations')->where('author_id', $user3->id)->first(); 16 | 17 | $conversations_users = array( 18 | array( 19 | 'user_id' => $user1->id, 20 | 'conversation_id' => $conversation1->id 21 | ), 22 | array( 23 | 'user_id' => $user2->id, 24 | 'conversation_id' => $conversation1->id 25 | ), 26 | array( 27 | 'user_id' => $user1->id, 28 | 'conversation_id' => $conversation2->id 29 | ), 30 | array( 31 | 'user_id' => $user3->id, 32 | 'conversation_id' => $conversation2->id 33 | ), 34 | array( 35 | 'user_id' => $user4->id, 36 | 'conversation_id' => $conversation2->id 37 | ) 38 | ); 39 | 40 | DB::table('conversations_users')->insert($conversations_users); 41 | } 42 | } -------------------------------------------------------------------------------- /app/database/seeds/DatabaseSeeder.php: -------------------------------------------------------------------------------- 1 | call('UsersTableSeeder'); 15 | $this->call('ConversationsTableSeeder'); 16 | $this->call('ConversationsUsersTableSeeder'); 17 | $this->call('MessagesTableSeeder'); 18 | $this->call('MessagesNotificationsTableSeeder'); 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /app/database/seeds/MessagesNotificationsTableSeeder.php: -------------------------------------------------------------------------------- 1 | delete(); 8 | 9 | $user1 = DB::table('users')->where('username', 'heisenberg')->first(); 10 | $user2 = DB::table('users')->where('username', 'pinkman')->first(); 11 | $user3 = DB::table('users')->where('username', 'skyler')->first(); 12 | $user4 = DB::table('users')->where('username', 'hank')->first(); 13 | 14 | $message1 = DB::table('messages')->where('body', 'Jesse!')->first(); 15 | $message2 = DB::table('messages')->where('body', 'Yo, bitch!')->first(); 16 | $message3 = DB::table('messages')->where('body', 'Tell him, Walt!')->first(); 17 | $message4 = DB::table('messages')->where('body', 'Do what you\'re gonna do.')->first(); 18 | $message5 = DB::table('messages')->where('body', 'Fuck you!')->first(); 19 | 20 | $messages_notifications = array( 21 | array( 22 | 'user_id' => $user2->id, 23 | 'message_id' => $message1->id, 24 | 'conversation_id' => $message1->conversation_id, 25 | 'read' => true, 26 | 'created_at' => new DateTime, 27 | 'updated_at' => new DateTime, 28 | ), 29 | array( 30 | 'user_id' => $user1->id, 31 | 'message_id' => $message2->id, 32 | 'conversation_id' => $message2->conversation_id, 33 | 'read' => true, 34 | 'created_at' => new DateTime, 35 | 'updated_at' => new DateTime, 36 | ), 37 | array( 38 | 'user_id' => $user1->id, 39 | 'message_id' => $message3->id, 40 | 'conversation_id' => $message3->conversation_id, 41 | 'read' => true, 42 | 'created_at' => new DateTime, 43 | 'updated_at' => new DateTime, 44 | ), 45 | array( 46 | 'user_id' => $user4->id, 47 | 'message_id' => $message3->id, 48 | 'conversation_id' => $message3->conversation_id, 49 | 'read' => true, 50 | 'created_at' => new DateTime, 51 | 'updated_at' => new DateTime, 52 | ), 53 | array( 54 | 'user_id' => $user1->id, 55 | 'message_id' => $message4->id, 56 | 'conversation_id' => $message4->conversation_id, 57 | 'read' => true, 58 | 'created_at' => new DateTime, 59 | 'updated_at' => new DateTime, 60 | ), 61 | array( 62 | 'user_id' => $user3->id, 63 | 'message_id' => $message4->id, 64 | 'conversation_id' => $message4->conversation_id, 65 | 'read' => true, 66 | 'created_at' => new DateTime, 67 | 'updated_at' => new DateTime, 68 | ), 69 | array( 70 | 'user_id' => $user4->id, 71 | 'message_id' => $message5->id, 72 | 'conversation_id' => $message5->conversation_id, 73 | 'read' => true, 74 | 'created_at' => new DateTime, 75 | 'updated_at' => new DateTime, 76 | ), 77 | array( 78 | 'user_id' => $user3->id, 79 | 'message_id' => $message5->id, 80 | 'conversation_id' => $message5->conversation_id, 81 | 'read' => true, 82 | 'created_at' => new DateTime, 83 | 'updated_at' => new DateTime, 84 | ) 85 | ); 86 | 87 | DB::table('messages_notifications')->insert($messages_notifications); 88 | } 89 | } -------------------------------------------------------------------------------- /app/database/seeds/MessagesTableSeeder.php: -------------------------------------------------------------------------------- 1 | delete(); 8 | 9 | $user1 = DB::table('users')->where('username', 'heisenberg')->first(); 10 | $user2 = DB::table('users')->where('username', 'pinkman')->first(); 11 | $user3 = DB::table('users')->where('username', 'skyler')->first(); 12 | $user4 = DB::table('users')->where('username', 'hank')->first(); 13 | 14 | $conversation1 = DB::table('conversations')->where('author_id', $user1->id)->first(); 15 | $conversation2 = DB::table('conversations')->where('author_id', $user3->id)->first(); 16 | 17 | $messages = array( 18 | array( 19 | 'created_at' => new DateTime, 20 | 'body' => 'Jesse!', 21 | 'conversation_id' => $conversation1->id, 22 | 'user_id' => $user1->id 23 | ), 24 | array( 25 | 'created_at' => new DateTime, 26 | 'body' => 'Yo, bitch!', 27 | 'conversation_id' => $conversation1->id, 28 | 'user_id' => $user2->id 29 | ), 30 | array( 31 | 'created_at' => new DateTime, 32 | 'body' => 'Tell him, Walt!', 33 | 'conversation_id' => $conversation2->id, 34 | 'user_id' => $user3->id 35 | ), 36 | array( 37 | 'created_at' => new DateTime, 38 | 'body' => 'Do what you\'re gonna do.', 39 | 'conversation_id' => $conversation2->id, 40 | 'user_id' => $user4->id 41 | ), 42 | array( 43 | 'created_at' => new DateTime, 44 | 'body' => 'Fuck you!', 45 | 'conversation_id' => $conversation2->id, 46 | 'user_id' => $user1->id 47 | ) 48 | ); 49 | 50 | DB::table('messages')->insert($messages); 51 | } 52 | } -------------------------------------------------------------------------------- /app/database/seeds/UsersTableSeeder.php: -------------------------------------------------------------------------------- 1 | delete(); 7 | 8 | $users = array( 9 | array( 10 | 'email' => 'heisenberg@gmail.com', 11 | 'username' => 'heisenberg', 12 | 'password' => Hash::make('heisenberg'), 13 | 'image_path' => '/img/heisenberg.jpg', 14 | 'created_at' => new DateTime, 15 | 'updated_at' => new DateTime 16 | ), 17 | array( 18 | 'email' => 'pinkman@gmail.com', 19 | 'username' => 'pinkman', 20 | 'password' => Hash::make('pinkman'), 21 | 'image_path' => '/img/pinkman.jpg', 22 | 'created_at' => new DateTime, 23 | 'updated_at' => new DateTime 24 | ), 25 | array( 26 | 'email' => 'skyler@gmail.com', 27 | 'username' => 'skyler', 28 | 'password' => Hash::make('skyler'), 29 | 'image_path' => '/img/skyler.jpg', 30 | 'created_at' => new DateTime, 31 | 'updated_at' => new DateTime 32 | ), 33 | array( 34 | 'email' => 'hank@gmail.com', 35 | 'username' => 'hank', 36 | 'password' => Hash::make('hankdea'), 37 | 'image_path' => '/img/hank.jpg', 38 | 'created_at' => new DateTime, 39 | 'updated_at' => new DateTime 40 | ), 41 | array( 42 | 'email' => 'gusfring@gmail.com', 43 | 'username' => 'gusfring', 44 | 'password' => Hash::make('gusfring'), 45 | 'image_path' => '/img/gusfring.jpg', 46 | 'created_at' => new DateTime, 47 | 'updated_at' => new DateTime 48 | ) 49 | ); 50 | 51 | DB::table('users')->insert($users); 52 | } 53 | } -------------------------------------------------------------------------------- /app/events/ChatConversationsEventHandler.php: -------------------------------------------------------------------------------- 1 | publish(self::CHANNEL, $data); 14 | } 15 | } -------------------------------------------------------------------------------- /app/events/ChatMessagesEventHandler.php: -------------------------------------------------------------------------------- 1 | publish(self::CHANNEL, $data); 14 | } 15 | } -------------------------------------------------------------------------------- /app/filters.php: -------------------------------------------------------------------------------- 1 | '« Previous', 17 | 18 | 'next' => 'Next »', 19 | 20 | ); 21 | -------------------------------------------------------------------------------- /app/lang/en/reminders.php: -------------------------------------------------------------------------------- 1 | "Passwords must be at least six characters and match the confirmation.", 17 | 18 | "user" => "We can't find a user with that e-mail address.", 19 | 20 | "token" => "This password reset token is invalid.", 21 | 22 | "sent" => "Password reminder sent!", 23 | 24 | "reset" => "Password has been reset!", 25 | 26 | ); 27 | -------------------------------------------------------------------------------- /app/lang/en/validation.php: -------------------------------------------------------------------------------- 1 | "The :attribute must be accepted.", 17 | "active_url" => "The :attribute is not a valid URL.", 18 | "after" => "The :attribute must be a date after :date.", 19 | "alpha" => "The :attribute may only contain letters.", 20 | "alpha_dash" => "The :attribute may only contain letters, numbers, and dashes.", 21 | "alpha_num" => "The :attribute may only contain letters and numbers.", 22 | "array" => "The :attribute must be an array.", 23 | "before" => "The :attribute must be a date before :date.", 24 | "between" => array( 25 | "numeric" => "The :attribute must be between :min and :max.", 26 | "file" => "The :attribute must be between :min and :max kilobytes.", 27 | "string" => "The :attribute must be between :min and :max characters.", 28 | "array" => "The :attribute must have between :min and :max items.", 29 | ), 30 | "boolean" => "The :attribute field must be true or false.", 31 | "confirmed" => "The :attribute confirmation does not match.", 32 | "date" => "The :attribute is not a valid date.", 33 | "date_format" => "The :attribute does not match the format :format.", 34 | "different" => "The :attribute and :other must be different.", 35 | "digits" => "The :attribute must be :digits digits.", 36 | "digits_between" => "The :attribute must be between :min and :max digits.", 37 | "email" => "The :attribute must be a valid email address.", 38 | "exists" => "The selected :attribute is invalid.", 39 | "image" => "The :attribute must be an image.", 40 | "in" => "The selected :attribute is invalid.", 41 | "integer" => "The :attribute must be an integer.", 42 | "ip" => "The :attribute must be a valid IP address.", 43 | "max" => array( 44 | "numeric" => "The :attribute may not be greater than :max.", 45 | "file" => "The :attribute may not be greater than :max kilobytes.", 46 | "string" => "The :attribute may not be greater than :max characters.", 47 | "array" => "The :attribute may not have more than :max items.", 48 | ), 49 | "mimes" => "The :attribute must be a file of type: :values.", 50 | "min" => array( 51 | "numeric" => "The :attribute must be at least :min.", 52 | "file" => "The :attribute must be at least :min kilobytes.", 53 | "string" => "The :attribute must be at least :min characters.", 54 | "array" => "The :attribute must have at least :min items.", 55 | ), 56 | "not_in" => "The selected :attribute is invalid.", 57 | "numeric" => "The :attribute must be a number.", 58 | "regex" => "The :attribute format is invalid.", 59 | "required" => "The :attribute field is required.", 60 | "required_if" => "The :attribute field is required when :other is :value.", 61 | "required_with" => "The :attribute field is required when :values is present.", 62 | "required_with_all" => "The :attribute field is required when :values is present.", 63 | "required_without" => "The :attribute field is required when :values is not present.", 64 | "required_without_all" => "The :attribute field is required when none of :values are present.", 65 | "same" => "The :attribute and :other must match.", 66 | "size" => array( 67 | "numeric" => "The :attribute must be :size.", 68 | "file" => "The :attribute must be :size kilobytes.", 69 | "string" => "The :attribute must be :size characters.", 70 | "array" => "The :attribute must contain :size items.", 71 | ), 72 | "unique" => "The :attribute has already been taken.", 73 | "url" => "The :attribute format is invalid.", 74 | "timezone" => "The :attribute must be a valid zone.", 75 | 76 | /* 77 | |-------------------------------------------------------------------------- 78 | | Custom Validation Language Lines 79 | |-------------------------------------------------------------------------- 80 | | 81 | | Here you may specify custom validation messages for attributes using the 82 | | convention "attribute.rule" to name the lines. This makes it quick to 83 | | specify a specific custom language line for a given attribute rule. 84 | | 85 | */ 86 | 87 | 'custom' => array( 88 | 'attribute-name' => array( 89 | 'rule-name' => 'custom-message', 90 | ), 91 | ), 92 | 93 | /* 94 | |-------------------------------------------------------------------------- 95 | | Custom Validation Attributes 96 | |-------------------------------------------------------------------------- 97 | | 98 | | The following language lines are used to swap attribute place-holders 99 | | with something more reader friendly such as E-Mail Address instead 100 | | of "email". This simply helps us make messages a little cleaner. 101 | | 102 | */ 103 | 104 | 'attributes' => array(), 105 | 106 | ); 107 | -------------------------------------------------------------------------------- /app/listeners.php: -------------------------------------------------------------------------------- 1 | belongsToMany('User', 'conversations_users', 'conversation_id', 'user_id')->where('user_id', '<>', Auth::user()->id); 12 | } 13 | 14 | public function messages() { 15 | return $this->hasMany('Message', 'conversation_id', 'id'); 16 | } 17 | 18 | public function messagesNotifications() { 19 | return $this->hasMany('MessageNotification', 'conversation_id', 'id')->where('read', 0)->where('user_id', Auth::user()->id); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/models/ConversationUser.php: -------------------------------------------------------------------------------- 1 | belongsTo('User', 'user_id'); 13 | } 14 | 15 | public function conversation() 16 | { 17 | return $this->belongsTo('Conversation', 'conversation_id'); 18 | } 19 | 20 | public function messages_notifications() { 21 | return $this->hasMany('MessageNotification', 'message_id', 'id'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/models/MessageNotification.php: -------------------------------------------------------------------------------- 1 | belongsToMany('Conversation', 'conversations_users', 'user_id', 'conversation_id'); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/routes.php: -------------------------------------------------------------------------------- 1 | 'auth.getLogin', 23 | 'uses' => 'AuthController@getLogin' 24 | )); 25 | 26 | Route::post('/login', array( 27 | 'as' => 'auth.postLogin', 28 | 'uses' => 'AuthController@postLogin' 29 | )); 30 | 31 | Route::get('/logout', array( 32 | 'as' => 'auth.logout', 33 | 'uses' => 'AuthController@logout' 34 | )); 35 | 36 | Route::get('/chat/', array( 37 | 'before' => 'auth', 38 | 'as' => 'chat.index', 39 | 'uses' => 'ChatController@index' 40 | )); 41 | 42 | Route::get('/messages/', array( 43 | 'before' => 'auth', 44 | 'as' => 'messages.index', 45 | 'uses' => 'MessageController@index' 46 | )); 47 | 48 | Route::post('/messages/', array( 49 | 'before' => 'auth', 50 | 'as' => 'messages.store', 51 | 'uses' => 'MessageController@store' 52 | )); 53 | 54 | Route::get('users/{user_id}/conversations', array( 55 | 'before' => 'auth', 56 | 'as' => 'conversations_users.index', 57 | 'uses' => 'ConversationUserController@index' 58 | )); 59 | 60 | Route::post('/conversations/', array( 61 | 'before' => 'auth', 62 | 'as' => 'conversations.store', 63 | 'uses' => 'ConversationController@store' 64 | )); 65 | 66 | Route::get('/conversations/', array( 67 | 'before' => 'auth', 68 | 'as' => 'conversations.index', 69 | 'uses' => 'ConversationController@index' 70 | )); 71 | 72 | -------------------------------------------------------------------------------- /app/start/artisan.php: -------------------------------------------------------------------------------- 1 | client->request('GET', '/'); 13 | 14 | $this->assertRedirectedTo('login'); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /app/tests/TestCase.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |

Password Reset

8 | 9 |
10 | To reset your password, complete this form: {{ URL::to('password/reset', array($token)) }}.
11 | This link will expire in {{ Config::get('auth.reminder.expire', 60) }} minutes. 12 |
13 | 14 | 15 | -------------------------------------------------------------------------------- /app/views/layouts/main.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Realtime Chat w/ Laravel 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | @yield('body') 15 | 16 | 17 | 18 | 19 | 20 | 21 | @yield('scripts') 22 | 23 | -------------------------------------------------------------------------------- /app/views/templates/chat.blade.php: -------------------------------------------------------------------------------- 1 | @extends('layouts/main') 2 | 3 | @section('body') 4 | 34 |
35 |
36 |
37 | New Message 38 |
39 |
40 |
41 |
42 | @include('templates/conversations', array('conversations' => $conversations, 'current_conversation' => $current_conversation)) 43 |
44 |
45 | @if($current_conversation) 46 |
47 |
48 | @include('templates/messages', array('messages' => $current_conversation->messages)) 49 |
50 |
51 | {{ Form::open(array('action' => 'MessageController@store')) }} 52 | 53 |
54 | Send Message 55 |
56 | {{ Form::close() }} 57 | @endif 58 |
59 |
60 |
61 | @stop 62 | 63 | @section('scripts') 64 | 69 | 70 | @stop 71 | 72 | @include('templates/new_message_modal', array('recipients' => $recipients)) 73 | -------------------------------------------------------------------------------- /app/views/templates/conversations.blade.php: -------------------------------------------------------------------------------- 1 | 21 | -------------------------------------------------------------------------------- /app/views/templates/login.blade.php: -------------------------------------------------------------------------------- 1 | @extends('layouts/main') 2 | 3 | @section('body') 4 |
5 |
6 |
7 |

Welcome to Realtime Chat!

8 |
9 | {{ Form::open(array('action' => 'AuthController@postLogin')) }} 10 |
11 | {{ Form::label('email', 'E-mail', array('class' => 'control-label')) }} 12 | {{ Form::text('email', null, array( 'class' => 'form-control')) }} 13 | (e.g., heisenberg@gmail.com, pinkman@gmail.com) 14 |
15 |
16 | {{ Form::label('password','Password', array('class' => 'control-label')) }} 17 | {{ Form::password('password', array('class' => 'form-control')) }} 18 | (e.g., heisenberg, pinkman) 19 |
20 |
21 | 22 |
23 | {{ Form::close() }} 24 | @if($errors) 25 |
    26 | @foreach ($errors->all() as $error) 27 |
  • {{ $error }}
  • 28 | @endforeach 29 |
30 | @endif 31 |
32 |
33 |
34 | @stop 35 | -------------------------------------------------------------------------------- /app/views/templates/messages.blade.php: -------------------------------------------------------------------------------- 1 | @foreach($messages as $message) 2 |
3 |
4 | {{ $message->created_at }} 5 | 6 | 7 | 8 |
9 |
{{ $message->user->username }}
10 | {{ $message->body }} 11 |
12 |
13 |
14 | @endforeach 15 | -------------------------------------------------------------------------------- /app/views/templates/new_message_modal.blade.php: -------------------------------------------------------------------------------- 1 | 27 | -------------------------------------------------------------------------------- /artisan: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | setRequestForConsoleEnvironment(); 45 | 46 | $artisan = Illuminate\Console\Application::start($app); 47 | 48 | /* 49 | |-------------------------------------------------------------------------- 50 | | Run The Artisan Application 51 | |-------------------------------------------------------------------------- 52 | | 53 | | When we run the console application, the current CLI command will be 54 | | executed in this console and the response sent back to a terminal 55 | | or another output device for the developers. Here goes nothing! 56 | | 57 | */ 58 | 59 | $status = $artisan->run(); 60 | 61 | /* 62 | |-------------------------------------------------------------------------- 63 | | Shutdown The Application 64 | |-------------------------------------------------------------------------- 65 | | 66 | | Once Artisan has finished running. We will fire off the shutdown events 67 | | so that any final work may be done by the application before we shut 68 | | down the process. This is the last thing to happen to the request. 69 | | 70 | */ 71 | 72 | $app->shutdown(); 73 | 74 | exit($status); 75 | -------------------------------------------------------------------------------- /bootstrap/autoload.php: -------------------------------------------------------------------------------- 1 | __DIR__.'/../app', 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Public Path 21 | |-------------------------------------------------------------------------- 22 | | 23 | | The public path contains the assets for your web application, such as 24 | | your JavaScript and CSS files, and also contains the primary entry 25 | | point for web requests into these applications from the outside. 26 | | 27 | */ 28 | 29 | 'public' => __DIR__.'/../public', 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Base Path 34 | |-------------------------------------------------------------------------- 35 | | 36 | | The base path is the root of the Laravel installation. Most likely you 37 | | will not need to change this value. But, if for some wild reason it 38 | | is necessary you will do so here, just proceed with some caution. 39 | | 40 | */ 41 | 42 | 'base' => __DIR__.'/..', 43 | 44 | /* 45 | |-------------------------------------------------------------------------- 46 | | Storage Path 47 | |-------------------------------------------------------------------------- 48 | | 49 | | The storage path is used by Laravel to store cached Blade views, logs 50 | | and other pieces of information. You may modify the path here when 51 | | you want to change the location of this directory for your apps. 52 | | 53 | */ 54 | 55 | 'storage' => __DIR__.'/../app/storage', 56 | 57 | ); 58 | -------------------------------------------------------------------------------- /bootstrap/start.php: -------------------------------------------------------------------------------- 1 | detectEnvironment(array( 28 | 'local' => array('*.dev', gethostname()), 29 | 'production' => array('*.com', '*.net', 'www.somedomain.com') 30 | )); 31 | 32 | /* 33 | |-------------------------------------------------------------------------- 34 | | Bind Paths 35 | |-------------------------------------------------------------------------- 36 | | 37 | | Here we are binding the paths configured in paths.php to the app. You 38 | | should not be changing these here. If you need to change these you 39 | | may do so within the paths.php file and they will be bound here. 40 | | 41 | */ 42 | 43 | $app->bindInstallPaths(require __DIR__.'/paths.php'); 44 | 45 | /* 46 | |-------------------------------------------------------------------------- 47 | | Load The Application 48 | |-------------------------------------------------------------------------- 49 | | 50 | | Here we will load this Illuminate application. We will keep this in a 51 | | separate location so we can isolate the creation of an application 52 | | from the actual running of the application with a given request. 53 | | 54 | */ 55 | 56 | $framework = $app['path.base']. 57 | '/vendor/laravel/framework/src'; 58 | 59 | require $framework.'/Illuminate/Foundation/start.php'; 60 | 61 | /* 62 | |-------------------------------------------------------------------------- 63 | | Return The Application 64 | |-------------------------------------------------------------------------- 65 | | 66 | | This script returns the application instance. The instance is given to 67 | | the calling script so we can separate the building of the instances 68 | | from the actual running of the application and sending responses. 69 | | 70 | */ 71 | 72 | return $app; 73 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel/laravel", 3 | "description": "The Laravel Framework.", 4 | "keywords": ["framework", "laravel"], 5 | "license": "MIT", 6 | "type": "project", 7 | "require": { 8 | "laravel/framework": "4.2.*", 9 | "phpunit/phpunit": "4.8.*@dev" 10 | }, 11 | "autoload": { 12 | "classmap": [ 13 | "app/commands", 14 | "app/controllers", 15 | "app/models", 16 | "app/database/migrations", 17 | "app/database/seeds", 18 | "app/tests/TestCase.php", 19 | "app/events" 20 | ], 21 | "psr-4": { 22 | "LaravelRealtimeChat\\": "app/LaravelRealtimeChat" 23 | } 24 | 25 | }, 26 | "scripts": { 27 | "post-install-cmd": [ 28 | "php artisan clear-compiled", 29 | "php artisan optimize" 30 | ], 31 | "post-update-cmd": [ 32 | "php artisan clear-compiled", 33 | "php artisan optimize" 34 | ], 35 | "post-create-project-cmd": [ 36 | "php artisan key:generate" 37 | ] 38 | }, 39 | "config": { 40 | "preferred-install": "dist" 41 | }, 42 | "minimum-stability": "stable" 43 | } 44 | -------------------------------------------------------------------------------- /dump.rdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guilhermeslk/laravel-realtime-chat/0da3a88e6e5d0f71774f43ea4206e576f45d51a1/dump.rdb -------------------------------------------------------------------------------- /nodejs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Realtime Chat Server 7 | 8 | 9 | 10 | 11 | 12 | 13 | 43 | 94 | 95 | 96 | 97 |

Realtime Chat Server Monitor

98 |
99 |
100 |
101 | 102 | -------------------------------------------------------------------------------- /nodejs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "realtime-chat", 3 | "version": "0.0.1", 4 | "description": "Realtime chat w/ Laravel", 5 | "main": "server.js", 6 | "dependencies": { 7 | "redis": "~0.12.1", 8 | "express": "~4.10.2", 9 | "socket.io": "~1.2.0" 10 | }, 11 | "devDependencies": {}, 12 | "scripts": { 13 | "test": "echo \"Error: no test specified\" && exit 1", 14 | "start": "node server.js" 15 | }, 16 | "author": "Guilherme Solinscki", 17 | "license": "ISC" 18 | } 19 | -------------------------------------------------------------------------------- /nodejs/server.js: -------------------------------------------------------------------------------- 1 | var 2 | app = require('http').createServer(handler), 3 | io = require('socket.io')(app), 4 | redis = require('redis'), 5 | fs = require('fs'), 6 | redisClient = redis.createClient(); 7 | 8 | app.listen(3000); 9 | 10 | console.log('Realtime Chat Server running at http://127.0.0.1:3000/'); 11 | 12 | function handler (req, res) { 13 | fs.readFile(__dirname + '/index.html', function(err, data) { 14 | if(err) { 15 | res.writeHead(500); 16 | return res.end('Error loading index.html'); 17 | } 18 | res.writeHead(200); 19 | res.end(data); 20 | }); 21 | } 22 | 23 | /*** 24 | Redis Channels Subscribes 25 | ***/ 26 | redisClient.subscribe('chat.conversations'); 27 | redisClient.subscribe('chat.messages'); 28 | 29 | /*** 30 | Redis Events 31 | ***/ 32 | redisClient.on('message', function(channel, message) { 33 | var result = JSON.parse(message); 34 | 35 | io.to('admin').emit(channel, 'channel -> ' + channel + ' | room -> ' + result.room); 36 | io.to(result.room).emit(channel, result); 37 | }); 38 | 39 | /*** 40 | Socket.io Connection Event 41 | ***/ 42 | io.on('connection', function(socket) { 43 | socket.emit('welcome', { message: 'Welcome! Realtime Chat Server running at http://127.0.0.1:3000/'} ); 44 | 45 | /*** 46 | Socket.io Events 47 | ***/ 48 | 49 | socket.on('join', function(data) { 50 | socket.join(data.room); 51 | socket.emit('joined', { message: 'Joined room: ' + data.room }); 52 | }); 53 | }); 54 | -------------------------------------------------------------------------------- /npm-debug.log: -------------------------------------------------------------------------------- 1 | 0 info it worked if it ends with ok 2 | 1 verbose cli [ 'node', '/usr/local/bin/npm', 'install' ] 3 | 2 info using npm@1.3.24 4 | 3 info using node@v0.10.25 5 | 4 error install Couldn't read dependencies 6 | 5 error package.json ENOENT, open '/Users/guilhermesolinscki/Documents/Projetos/package.json' 7 | 5 error package.json This is most likely not a problem with npm itself. 8 | 5 error package.json npm can't find a package.json file in your current directory. 9 | 6 error System Darwin 14.0.0 10 | 7 error command "node" "/usr/local/bin/npm" "install" 11 | 8 error cwd /Users/guilhermesolinscki/Documents/Projetos/laravel-realtime-chat 12 | 9 error node -v v0.10.25 13 | 10 error npm -v 1.3.24 14 | 11 error path /Users/guilhermesolinscki/Documents/Projetos/package.json 15 | 12 error code ENOPACKAGEJSON 16 | 13 error errno 34 17 | 14 verbose exit [ 34, true ] 18 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./app/tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /public/.htaccess: -------------------------------------------------------------------------------- 1 | 2 | 3 | Options -MultiViews 4 | 5 | 6 | RewriteEngine On 7 | 8 | # Redirect Trailing Slashes... 9 | RewriteRule ^(.*)/$ /$1 [L,R=301] 10 | 11 | # Handle Front Controller... 12 | RewriteCond %{REQUEST_FILENAME} !-d 13 | RewriteCond %{REQUEST_FILENAME} !-f 14 | RewriteRule ^ index.php [L] 15 | 16 | -------------------------------------------------------------------------------- /public/css/chat.css: -------------------------------------------------------------------------------- 1 | .user-picture { 2 | margin-right: 10px; 3 | } 4 | 5 | .messages-panel { 6 | height: 400px; 7 | overflow-y: scroll; 8 | } 9 | 10 | .send-message { 11 | margin-top: 5px; 12 | } 13 | 14 | .new-message { 15 | margin-bottom: 5px; 16 | } 17 | 18 | .message { 19 | margin: 10px; 20 | } -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guilhermeslk/laravel-realtime-chat/0da3a88e6e5d0f71774f43ea4206e576f45d51a1/public/favicon.ico -------------------------------------------------------------------------------- /public/img/gusfring.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guilhermeslk/laravel-realtime-chat/0da3a88e6e5d0f71774f43ea4206e576f45d51a1/public/img/gusfring.jpg -------------------------------------------------------------------------------- /public/img/hank.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guilhermeslk/laravel-realtime-chat/0da3a88e6e5d0f71774f43ea4206e576f45d51a1/public/img/hank.jpg -------------------------------------------------------------------------------- /public/img/heisenberg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guilhermeslk/laravel-realtime-chat/0da3a88e6e5d0f71774f43ea4206e576f45d51a1/public/img/heisenberg.jpg -------------------------------------------------------------------------------- /public/img/pinkman.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guilhermeslk/laravel-realtime-chat/0da3a88e6e5d0f71774f43ea4206e576f45d51a1/public/img/pinkman.jpg -------------------------------------------------------------------------------- /public/img/skyler.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guilhermeslk/laravel-realtime-chat/0da3a88e6e5d0f71774f43ea4206e576f45d51a1/public/img/skyler.jpg -------------------------------------------------------------------------------- /public/index.php: -------------------------------------------------------------------------------- 1 | 7 | */ 8 | 9 | /* 10 | |-------------------------------------------------------------------------- 11 | | Register The Auto Loader 12 | |-------------------------------------------------------------------------- 13 | | 14 | | Composer provides a convenient, automatically generated class loader 15 | | for our application. We just need to utilize it! We'll require it 16 | | into the script here so that we do not have to worry about the 17 | | loading of any our classes "manually". Feels great to relax. 18 | | 19 | */ 20 | 21 | require __DIR__.'/../bootstrap/autoload.php'; 22 | 23 | /* 24 | |-------------------------------------------------------------------------- 25 | | Turn On The Lights 26 | |-------------------------------------------------------------------------- 27 | | 28 | | We need to illuminate PHP development, so let's turn on the lights. 29 | | This bootstraps the framework and gets it ready for use, then it 30 | | will load up this application so that we can run it and send 31 | | the responses back to the browser and delight these users. 32 | | 33 | */ 34 | 35 | $app = require_once __DIR__.'/../bootstrap/start.php'; 36 | 37 | /* 38 | |-------------------------------------------------------------------------- 39 | | Run The Application 40 | |-------------------------------------------------------------------------- 41 | | 42 | | Once we have the application, we can simply call the run method, 43 | | which will execute the request and send the response back to 44 | | the client's browser allowing them to enjoy the creative 45 | | and wonderful application we have whipped up for them. 46 | | 47 | */ 48 | 49 | $app->run(); 50 | -------------------------------------------------------------------------------- /public/js/chat.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | 3 | /*** 4 | Initialization 5 | ***/ 6 | 7 | scrollToBottom(); 8 | 9 | var 10 | socket = io('http://localhost:3000'), 11 | 12 | jqxhr = $.ajax({ 13 | url: '/users/' + user_id + '/conversations', 14 | type: 'GET', 15 | dataType: 'json' 16 | }); 17 | 18 | jqxhr.done(function(data) { 19 | if(data.success && data.result.length > 0) { 20 | $.each(data.result, function(index, conversation) { 21 | socket.emit('join', { room: conversation.name }); 22 | }); 23 | } 24 | }); 25 | 26 | /*** 27 | Socket.io Events 28 | ***/ 29 | 30 | socket.on('welcome', function (data) { 31 | console.log(data.message); 32 | 33 | socket.emit('join', { room: user_id }); 34 | }); 35 | 36 | socket.on('joined', function(data) { 37 | console.log(data.message); 38 | }); 39 | 40 | socket.on('chat.messages', function(data) { 41 | var 42 | $messageList = $("#messageList"), 43 | $conversation = $("#" + data.room); 44 | 45 | var message = data.message.body, 46 | from_user_id = data.message.user_id, 47 | conversation = data.room; 48 | 49 | getMessages(conversation).done(function(data) { 50 | 51 | $conversation.find('small').text(message); 52 | 53 | if(conversation === current_conversation) { 54 | $messageList.html(data); 55 | scrollToBottom(); 56 | } 57 | 58 | if(from_user_id !== user_id && conversation !== current_conversation) { 59 | updateConversationCounter($conversation); 60 | } 61 | }); 62 | }); 63 | 64 | socket.on('chat.conversations', function(data) { 65 | var $conversationList = $("#conversationList"); 66 | 67 | getConversations(current_conversation).done(function(data) { 68 | $conversationList.html(data); 69 | }); 70 | }); 71 | 72 | /*** 73 | Functions 74 | ***/ 75 | 76 | function getConversations(current_conversation) { 77 | var jqxhr = $.ajax({ 78 | url: '/conversations', 79 | type: 'GET', 80 | data: { conversation: current_conversation }, 81 | dataType: 'html' 82 | }); 83 | 84 | return jqxhr; 85 | } 86 | 87 | function getMessages(conversation) { 88 | var jqxhr = $.ajax({ 89 | url: '/messages', 90 | type: 'GET', 91 | data: { conversation: conversation }, 92 | dataType: 'html' 93 | }); 94 | 95 | return jqxhr; 96 | } 97 | 98 | function sendMessage(body, conversation, user_id) { 99 | var jqxhr = $.ajax({ 100 | url: '/messages', 101 | type: 'POST', 102 | data: { body: body , conversation: conversation, user_id: user_id }, 103 | dataType: 'json' 104 | }); 105 | 106 | return jqxhr; 107 | } 108 | 109 | function updateConversationCounter($conversation) { 110 | var 111 | $badge = $conversation.find('.badge'), 112 | counter = Number($badge .text()); 113 | 114 | if($badge.length) { 115 | $badge.text(counter + 1); 116 | } else { 117 | $conversation.prepend('1'); 118 | } 119 | } 120 | 121 | function scrollToBottom() { 122 | var $messageList = $("#messageList"); 123 | 124 | if($messageList.length) { 125 | $messageList.animate({scrollTop: $messageList[0].scrollHeight}, 500); 126 | } 127 | } 128 | 129 | /*** 130 | Events 131 | ***/ 132 | 133 | $('#btnSendMessage').on('click', function (evt) { 134 | var $messageBox = $("#messageBox"); 135 | 136 | evt.preventDefault(); 137 | 138 | sendMessage($messageBox.val(), current_conversation, user_id).done(function(data) { 139 | console.log(data); 140 | $messageBox.val(''); 141 | $messageBox.focus(); 142 | }); 143 | }); 144 | 145 | $('#btnNewMessage').on('click', function() { 146 | $('#newMessageModal').modal('show'); 147 | }); 148 | 149 | /** 150 | * Shift+Enter to send message 151 | */ 152 | $('#messageBox').keypress(function (event) { 153 | if (event.keyCode == 13 && event.shiftKey) { 154 | event.preventDefault(); 155 | 156 | $('#btnSendMessage').trigger('click'); 157 | } 158 | }); 159 | }); 160 | -------------------------------------------------------------------------------- /public/packages/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guilhermeslk/laravel-realtime-chat/0da3a88e6e5d0f71774f43ea4206e576f45d51a1/public/packages/.gitkeep -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | User-agent: * 2 | Disallow: 3 | -------------------------------------------------------------------------------- /realtime.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | clear 3 | 4 | node nodejs/server.js 5 | -------------------------------------------------------------------------------- /server.php: -------------------------------------------------------------------------------- 1 |