I am trying to make some information display in real-time on a site of mine. Followed this guide: http://www.volkomenjuist.nl/blog/2013/10/20/laravel-4-and-nodejsredis-pubsub-realtime-notifications/
Right now it seems to be working to a point. Having both Firefox and Chrome open, I can do the action on Chrome and Firefox's console logs it instantly (awesome!!). However, I can't for the life of me figure out how to send a full array over to the event listener/socket.on function. In this case, the $val variable in that Event fire is the only value getting logged in the console.
Firing the Event:
Event::fire( UpdateHealthEventHandler::EVENT, [ $val, $slug ] );
Event listener:
Event::listen( UpdateHealthEventHandler::EVENT, 'UpdateHealthEventHandler' );
Clientside display with socket.io:
$( document ).ready( function()
{
var socket = io.connect('ws://localhost:8081/');
socket.on('health.update', function ( val, slug ) {
//Do something with data
console.log('updated: '+ val);
console.log('slug: '+ slug);
$( '.inner[data-name="'+ slug +'"]' ).fadeOut( 300, function() {
$( this ).html( val );
}).fadeIn( 300 );
});
});
Event handler class:
<?php
class UpdateHealthEventHandler {
CONST EVENT = 'health.update';
CONST CHANNEL = 'health.update';
public function handle( $data )
{
$redis = Redis::connection();
$redis->publish( self::CHANNEL, $data );
}
}
In other words, how do I pass both the $val and $slug variables to the listener/socket?
http://stackoverflow.com/questions/22001247/redis-how-to-store-associative-array-set-or-hash-or-list
I think that should answer your question
TL:DR seems like you should pass your array as a JSON string (possibly doing $redis->set())
Omg, thanks!! That was it :) After parsing the JSON via jquery I was able to post both values into the console log!
socket.on('val.update', function ( json ) {
//Do something with data
var data = JSON.parse( json );
console.log('updated: '+ data.val);
console.log('slug: '+ data.slug);
$( '.inner[data-name="'+ data.slug +'"]' ).fadeOut( 300, function() {
$( this ).html( data.val );
}).fadeIn( 300 );
});
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community