Reputation: 1434
I got a webapplication written in Laravel 4. This application makes use of Ratchet and to be more specific, it uses the package Latchet. As a sidenote I am using the following techniques :
Now I got the following scenario:
In my routes.php, I have the following code, so that a topic is registered correctly :
//routes.php
// Setup a connection and register a topic where clients can connect to.
Latchet::connection('Connection');
Latchet::topic('PhotoStream/{client}', 'PhotoStreamController');
Then, I start the ratchet server.
sudo php artisan latchet:listen
When a photo gets uploaded, I can then run the following code to push updates to the clients that are listening to my topic (PhotoStream/client1
in this case):
// Create the object, save it to db and then publish it to my websockets
$photo = new Photo;
$photo->location = 'path/to/file';
$photo->save();
// Publish it through my websocket clients. (push from server).
Latchet::publish('PhotoStream/client1', array('msg' => $photo->toArray() ));
This code all works, but it is in case of an update. My question is as follows:
How should I handle the initialisation of the client?
The latter of the two options seems the best option to me but I don't really know how to implement this in a good way.
Upvotes: 27
Views: 5648
Reputation: 800
If understood your question in the right way this is it: You are wondering if sending images over the websocket is a good idea if those images could also be preloaded form PHP.
I would suggest you to use PHP to preload the images without using the websocket and would start using the socket once new images are being added.
This way the user should see images from the moment the page is loaded and they will not have to wait for the websocket connection to be established.
If you prefer to do the loading over the socket I would still suggest you to load the first few images from the slider, that can be seen immediately, from PHP. Otherwise the user will have to wait longer(note that much, but noticeably longer still).
Upvotes: 0
Reputation: 1463
On the javascript side (to retrieve initial list):
//session.subscribe(....)
session.call('route/to/controller', arg1, arg2).then(function(res) {
console.log(res) //initial collection of photos
});
On the php side (to retrieve initial list):
public function call($connection, $id, $topic, $params) {
//the id is needed to be able to trace your async calls back to the right promise
$connection->callResult($id, $this->getInitialPhotosFilteredByParams($params));
});
Since you already successfully gotten updates via the subscribe, this is all you need. Watch out for xss though, params might not be filtred.
Upvotes: 4