1

I am trying to understand how this all push notifications works. I tried to do some test of push technology but so far i failed.

The base assumptions are:
1) use Apache web-server as the main application web-server (mandatory since all our code is using that)
2) Cross-Browser push notification server in node.js Technology (offered socket.io since it is crossed browser).

So far i failed and here is my code (p1.html):

<!doctype html>
<html>
<head>

<meta charset="UTF-8">
<title>P1</title>
</head>
<body>

<h1>P1</h1>
<section id="content"></section>
<script src="/socket.io.js"></script> <!--socket.io-->
<script src="/socket.js"></script> <!--socket.io-client-->
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>

var socket = io.connect('http://localhost:8080');

socket.on('notification', function (data) {
$('#content').append(data.message + '<br>')

});

</script>

</body>

</html>

and my server script (p1.js):

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, url = require('url')
app.listen(8080);

console.log("creating a connection");
io.sockets.on( 'connection', function ( socket ) {

console.log("runing time");
sendTimeMessage(socket);
});

function sendTimeMessage(socket){

console.log("in time");
var time= new Date().getTime();
console.log(time);
socket.volatile.emit( 'notification' , time );
setTimeout(sendTimeMessage, 5000);
}

function handler (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("");
}

function sendMessage(message) {

io.sockets.emit('notification', {'message': message});
}

  • i changed the IPs to local host for the example so i hope there is no mistake on the syntax.
  • when i run, the Apache web-server is the one that display the data and the idea is for the socket-io to update few fields.

current state:
1. If i don't add the socket.io-client js file i get reference error for socket.io-client
2. If i do add socket.io-client i get "ReferenceError: require is not defined [Break On This Error] 'undefined' != typeof io ? io : module.exports

i can really need help understanding it, and making it work. i am also open minded to alternative solutions i can really need help getting this done.

1
  • 1
    Well, I see that you don't have a semicolon at the end of require('url'). Commented Jan 10, 2013 at 11:27

3 Answers 3

2

Working example, of what you want to achieve. First mistake is wrong javascript path on client-side, the right one is /socket.io/socket.io.js. Second mistake is use of socket.volatile which doesn't exist.

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, url = require('url')


console.log("creating a connection");

io.sockets.on( 'connection', function ( socket ) {
  console.log("runing time");
  sendTimeMessage(socket);
});

function sendTimeMessage(socket){
  console.log("in time");
  var now= new Date().getTime();
  socket.emit('notification', {'message': now});
  setTimeout(function() {
    socket.emit('notification', {'message': "after 5s"});
  },5000);
}

function handler (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end("<html><script src=\"/socket.io/socket.io.js\"></script> <!--socket.io--><script>io.connect().on('notification', function (data) {console.log(data)});</script></html>");
}

app.listen(8080);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but... You are using the socket.io as the web-server and this is not what i am looking for. I have an Apache web-server that run certain code. i want to add an ability to my web-application. i don't want to replace (and debug all over again) my entire code. I want to add a socket.io notification server, that on certain pages receive a new connection and will be able to push data and update the code. if you look at my example, i have a page displayed by the Apache and i try to run the socket.io aside to the Apache. This "service" will do the push notification for me
2

Ok, i partially solved the with a huge help from the guys on IRC i created an: 1) HTML over Apache on port 80 2) live notification service update my HTML over port 8080

(there might still have code issue in the values arrived from the functions cause its not fully debuged)

p1.html (client)

<!doctype html>
<html>

<head>

<meta charset="UTF-8">
</head>
<body>
<section id="content"></section>
<script src="/node_modules/socket.io-client/dist/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>

var socket = io.connect('http://10.10.10.1:8080');

socket.on('notification', function (from,msg) {

$('#content').append(msg.message + '<br>')

});

</script>

</body>

</html>

p1.js (service)

var io = require('socket.io').listen(8080)
console.log("creating a connection");
io.sockets.on( 'connection', function ( socket ) {

console.log("runing time");
var oldtime= new Date().getTime();
while (1){

var newtime= new Date().getTime();
if (newtime%5323==0 && newtime != oldtime){

oldtime = newtime;
console.log(newtime);
socket.emit( 'notification' , {'message': "the time is - " + newtime} );

}

}

});

enjoy

Comments

0

Thanks #yanger I was helped by your code.

I want to add a comment. But I can't use comment yet.

In my case, I want to make a real time alarm. and I use 80 port web server and 81 port alarm server.

So I just use this code. (Client.js)

var socket = io.connect(':81');

It's totally working. I wish someone would read this article and get help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.