I have done many researches and it seems I can't find the proper solution. I am confident in PHP. I also have done some tutorials on Node.JS and Socket.IO and I'm currently learning Symfony2 but I can't see how I can merge the two to achieve my goal.
My goal is to set up real-time notification for back-end users of my app. This app is a e-commerce website and I want the admin behind the scenes to be warned as soon as an order is made by a visual notification in the upper right corner of the admin panel. My server use FreeBSD.
My plan is to use Node.JS and Socket.IO to achieve this. If there is a better plan, I'm willing to hear about it. Otherwise, I cannot find proper resources to tell me how I can include Node.JS and Socket.IO to a Symfony2 app. I use composer to install bundles but I haven't used NPM with Symfony2.
I have found this question, this link and this other question to help me out but none of these tell me how I can install Node.JS in a Symfony2 app.
If someone could help me with the steps to complete to make me start developping this feature, I'd be glad.
Thanks!
For those who might 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
Develop the code
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);
Front-end
<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>
Launch server
$ node app.js
That's all!
Related
i am new in node.js and i want to create chat system so anybody help me that how can i run my codes on local and on server
Server - (app.js):
var io = require('socket.io')(80);
io.on('connection', function (socket) {
socket.on('message', function () { });
socket.on('disconnect', function () { });
});
Client - (index.html):
<script>
var socket = io('http://localhost/');
socket.on('connect', function () {
socket.send('hi');
socket.on('message', function (msg) {
// my msg
});
});
</script>
what is server (app.js) and how can i run this on my server and how can i call this function to client ?
Step 1. Install node.
Download package from https://nodejs.org/en/, then you got npm and node file.
Create soft link to /usr/local/ for npm and node file.
Step 2. Crate your app.js, and typing:
node app.js
then, your code is running. And you can test in your frontend.
If you wanna your code running background, you can use pm2.
npm install -g pm2
After that, pm2 start app.js. Your code is running background.
See https://www.tutorialspoint.com/socket.io/socket.io_hello_world.htm
You need node installed on your server as a prerequisite.
Once you have nodejs installed, the app can be executed as node app.js
I have an Electron application running a Node Express server. The Electron main process is:
var express = require('express');
var app = express();
app.get('/', function() {
/* I want to use WebSQL here */
res.send('Hi !!!');
});
app.listen(3636, function() {
console.log('Listen ...');
});
var window = new BrowserWindow();
window.loadURL('http://localhost:3636');
I want to use WebSQL inside my Node Express controllers but, i tried with openDatabase command and obviouslly i received "undefined function" error.
Any ideas ?
The reason you get undefined function is that you're trying openDatabase in node and not in the browser. If you still really want to use websql but in node you can try out https://github.com/nolanlawson/node-websql#readme (npm install websql -S to add it to your package.json)
I am trying to learn the basics of node.js and socket.io. I have been using this tutorial http://tutorialzine.com/2012/08/nodejs-drawing-game/
the full code for this problem can be seen in the link above.
I can create a basic web server with node.js and get it to return hello world so I am sure that's installed correctly. However upon installing these packages
npm install socket.io#0.9.10 node-static
and setting up the serverside js as instructed
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static');
var fileServer = new nstatic.Server('./');
app.listen(8080);
I just get this prompt in my cmd and a constantly hanging web browser, instead of the html page that is meant to be served.I think I may have messed up an install but upon looking at the list of installed packages in npm it states both socket.io and node-static are present.
The code below should be more effective?, it looks like you are missing the handler part. The response must be explicitly ended or browser requests will hang forever like you are seeing. The node-static file.serve method manages the request once you pass it down. The source for .serve is here: https://github.com/cloudhead/node-static/blob/master/lib/node-static.js#L164
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static');
app.listen(8080);
var file = new nstatic.Server('./');
function handler(request, response) {
request.addListener('end', function () {
file.serve(request, response);
}).resume();
}
console.log('started')
Note also that the default file to serve to responses at / is index.html.
sorry for the silly question and im sure its a noob mistake. I am following the how to use for socket io: http://socket.io/#how-to-use and keep having an problem;
I have a node app (running version 3.0 alpha of express) and have the following:
app = express()
io = require('socket.io').listen app
I have edited the layout.jade and added:
script(src="/socket.io/socket.io.js")
I have also run npm install socket.io and confirmed that it starts fine on the server.
If I browse to any page, console keeps showing:
GET http://localhost:3000/socket.io/socket.io.js 404 (Not Found)
Anyone else experienced this issue?
Here are step by step instructions I just tested them
express socket
cd socket
npm install
npm install socket.io
Add the following in app.js
var io = require('socket.io').listen(app);
Run
node app
In the console you should see
info - socket.io started
In the browser go to
http://localhost:3000/socket.io/socket.io.js
You should see the raw JavaScript
Edit:
I'm also having problems with 3.0alpha1. Looks like a bug. Here is an ugly work around
var fs = require('fs');
app.get('/socket.io/socket.io.js', function(req, res) {
fs.readFile('/PROJECT_HOME/node_modules/socket.io/lib/socket.io.js', function(error, content) {
if (error) {
res.writeHead(500);
res.end();
}
else {
res.writeHead(200, { 'Content-Type': 'text/javascript' });
res.end(content, 'utf-8');
}
});
});
Are you runnning layout.jade from the app.js?
app.get('/',function(req,res){
res.sendfile('index.html');
});
This needs to be included in the app.js assuming your template is in index.html file.
My sio = require('socket.io').listen(app) is in my server.js file, but I'm calling a method in a library that would like to push a message to the client... say api.user.pushToClient()
How am I able to access sio.sockets from there? Perhaps my structure is incorrect?
Folder structure:
server.js
api
|--user.js
|--another.js
in server.js append this line
module.exports.sio = sio;
in api/user.js
sio = require('../server').sio;
sio.sockets.on ...
Or did I misunderstand the question?
What I understood from the question is you want to know how to use socketIO with node module.Based on my understanding you can use it as below:
First install socketIO module locally with npm by running " $npm install socket.io " command for windows.
Add Script to your HTML page:
<script src="/socket.io/socket.io.js"></script>
Now add var io = require('socket.io'); to your server or js file where you are going to use it.
Then you can have server startup code listen to that server and on connection of it perform the options for any event.
var listener = io.listen(server);
listener.sockets.on('connection', function(socket) {
socket.on('locationClick', function(data) {
// perform the function on receving locationClick event.
}
}