This project demonstrates how to add real-time functionality to a Symfony 2 application using three different real-time web technologies
It also adds support for sending an SMS with Nexmo.
These samples were originally prepare for a talk at Symfony Live London 2015 and then for CloudConf 2016. You can view the resources here:
- Real-time Web Apps & Symfony. What are your options? - Symfony Live 2015
- Real-time Web Apps & PHP. What are your options? - CloudConf 2016
Install the Symfony app dependencies:
cd symfony
composer install
cd ..
If you wish to run the Pusher sample you'll need to create a app/config/pusher.yml
and signup for a free account. If you don't wish to run the Pusher sample you will need to remove the import
from app/config/config.yml
.
Once the dependencies are installed you'll need to create the database for the sample chat application.
php symfony/app/console doctrine:database:create
php symfony/app/console doctrine:schema:update --force
Run the application:
php symfony/app/console server:run
For this sample to work you will need Redis installed.
Note: Redis only works on *nix machines
Install the Ratchet application dependencies:
cd ratchet
composer install
cd ..
Messages will be recieved by Ratchet from the Symfony application via Redis (Symfony -> Redis -> Ratchet
). So, you'll need to uncomment the code that publishes the chat messages to Redis.
Open up symfony/src/AppBundle/Controller/ChatController.php
and ensure the following is uncommented:
$data = [
'event' => 'new-message',
'data' => $message
];
$jsonContent = json_encode($data);
$redis = new Client('tcp://127.0.0.1:6379');
$redis->publish('chat', $jsonContent);
Ensure the Symfony application is running:
php symfony/app/console server:run
In a new console/terminal window ensure redis is running:
redis-server
In a new console/terminal run the Ratchet application:
php ratchet/bin/chat-server.php
Navigate to http://localhost:8000/chat/ratchet
. Open a 2nd browser window so you can see the messages appear in both windows.
For this sample to work you will need Redis installed.
Note: Redis only works on *nix machines
Install the Faye application dependencies:
cd faye
npm install
cd ..
Messages will be received by Faye from the Symfony application via Redis (Symfony -> Redis -> Faye
). So, you'll need to uncomment the code that publishes the chat messages to Redis.
Open up symfony/src/AppBundle/Controller/ChatController.php
and ensure the following is uncommented:
$data = [
'event' => 'new-message',
'data' => $message
];
$jsonContent = json_encode($data);
$redis = new Client('tcp://127.0.0.1:6379');
$redis->publish('chat', $jsonContent);
Ensure the Symfony application is running:
php symfony/app/console server:run
In a new console/terminal window ensure redis is running:
redis-server
In a new console/terminal run the Faye application:
node faye/index.js
Navigate to http://localhost:8000/chat/faye
. Open a 2nd browser window so you can see the messages appear in both windows.
For this sample to work you will need to signup for a free Pusher account.
Messages will be sent to Pusher and on to the web browser client via the Pusher service.
Open up symfony/src/AppBundle/Controller/ChatController.php
and ensure the following is uncommented:
$pusher = $this->container->get('lopi_pusher.pusher');
$pusher->trigger(
'chat',
'new-message',
$message
);
Note: If you've previously used the Ratchet or Faye sample then you should comment out the lines that interact with Redis.
Ensure the Symfony application is running:
php symfony/app/console server:run
Navigate to http://localhost:8000/chat/pusher
. Open a 2nd browser window so you can see the messages appear in both windows.
If you've any questions about this sample please raise and issue. If you've any more general questions then please email me: phil@pusher.com/phil@leggetter.co.uk.