Friday 2 June 2017

How does redis fit into the Laravel socket workflow?

I am currently busy with websockets and I have set up a node.js server which can receive calls like so:

const net = require('net');
const io = require('socket.io').listen(8000);
const ioClients = [];

const server = net.createServer((socket) => {
    socket.on('data', (msg) => {
        // Send the receive socket_connect message off to our socket.io script.
        ioClients.forEach((ioClient) => {
            ioClient.emit('log', msg.toString().trim());
        });
    });
}).listen(5600);

io.sockets.on('connection', function (socketIo) {
    ioClients.push(socketIo);
});

server.on('listening', () => {
    console.log('Websocket server online, listening on port :' + server.address().port);
});

To make calls I use php:

private function openSocketConnection($address = 'localhost', $port = 5600)
{
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    socket_connect($socket, $address, $port);
    return $socket;
}

socket_write($socket, $html, strlen($html));

In turn I listen for broadcasts that have been sent with the socket.io client on the page where it is needed.

This workflow seems to work pretty good and although I am really new at this it seems like a good direction.

I have implemented this inside Laravel but everywhere I go I see websocket tutorials and guides for Laravel that combine redis into this websocket workflow.

Why is Redis needed if the above examples work just fine what is the role and function of Redis in this workflow?



via Stephan-v

No comments:

Post a Comment