Enable Node.JS + Socket.IO in Symfony2 Application

I have done a lot of research, and it seems I can’t find the right solution. I am sure about PHP. I have also conducted several tutorials on Node.JSand Socket.IO, and now I am studying Symfony2, but I do not see how I can combine these two to achieve my goal.

My goal is to set up real-time notification for internal users of my application. This application is an e-commerce website, and I want the administrator backstage to be alerted as soon as the order is made by visual notification in the upper right corner of the admin panel. My server is using FreeBSD.

My plan is to use Node.JSand Socket.IOto achieve this. If there is a better plan, I want to hear about it. Otherwise, I can not find the right resources to tell me how to turn on Node.JSand Socket.IOin the app Symfony2. I use composer to install packages, but I have not used NPM with Symfony2.

I found this question , this link, and this other question to help me, but none of them tell me how I can install Node.JSin the application Symfony2.

If someone can help me with the steps so that I begin to develop this function, I would be happy.

Thank!

+4
source share
1 answer

For those who may be interested in the answer:

$ su -

Install Node.JS

$ cd /usr/ports/www/node

$ make install clean

Install NPM

$ cd /usr/ports/www/npm

$ make install clean

Install Socket.IO

$ cd /path/to/your/project/js/public/files

$ npm install socket.io

Code development

app.js

var http = require('http');
var fs = require('fs');

var server = http.createServer(function(req, res) {
    fs.readFile('./index.html', 'utf-8', function(error, content) {
        res.writeHead(200, {"Content-Type": "text/html"});
        res.end(content);
    });
});

var io = require('socket.io').listen(server);

io.sockets.on('connection', function (socket) {
    socket.on('newOrder', function () {
        socket.broadcast.emit('message', 'Nouvelle commande');
    });
});

server.listen(4321);

Frontal

<script src="{{ asset('http://localhost:4321/socket.io/socket.io.js') }}"></script>

<script>
    jQuery(function($) {
        var socket = io.connect('http://localhost:4321');

        $('form').on('submit', function() {
            socket.emit('newOrder', '1');
        });

    });
</script>

Back end

<script>
    jQuery(function($) {    
        var socket = io.connect('http://localhost:4321');

        socket.on('message', function(message) {
            alert(message);
        });
    });

</script>

Start server

$ node app.js

What all!

+8
source

All Articles