Skip to content

Examples of adding Real-Time functionality to a Symfony app using Ratchet (React PHP), Faye (Node.js) and Pusher

Notifications You must be signed in to change notification settings

radutopala/realtime-symfony-examples

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Real-Time & Symfony Samples

This project demonstrates how to add real-time functionality to a Symfony application using three different real-time technologies.

  1. Ratchet (PHP)
  2. Faye (Node)
  3. Pusher (Hosted service)

These samples were originally prepare for a talk at Symfony Live London 2015. You can view the slides here:

Symfony App Setup

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

Symfony + Ratchet

Symfony + Ratchet architecture overview

Dependencies

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 ..

Code Changes

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);

Running the App

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.

Symfony + Faye

Symfony + Faye architecture overview

Dependencies

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 ..

Code Changes

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);

Running the App

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.

Symfony + Pusher

Symfony + Pusher architecture overview

Dependencies

For this sample to work you will need to signup for a free Pusher account.

Code Changes

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.

Running the App

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.

Questions/Feedback

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.

About

Examples of adding Real-Time functionality to a Symfony app using Ratchet (React PHP), Faye (Node.js) and Pusher

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 81.1%
  • JavaScript 7.5%
  • HTML 5.7%
  • ApacheConf 5.5%
  • Shell 0.2%