Help with socket.io

187 views
Skip to first unread message

Phil Daws

unread,
Jan 26, 2012, 1:45:13 AM1/26/12
to nod...@googlegroups.com
Hello,

I am piecing together my first little app which is a web server then displays some content pulled from an Asterisk PBX server. The problem I am having is that I have split the asterisk code into one module, and the webserver into another, and then exported the start functions which get called from index.js but due to the io server being created in the web module how do I reference it from the asterisk module. Hope that makes sense ? I have uploaded the code on pastebin:


What should happen is that the div container in the HTML body receives the event data which has been emitted from the Asterisk event.

When I run node index.js I receive:

node.js:218
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
ReferenceError: server is not defined

Help would be gratefully appreciated.
--
Thanks, Phil

mscdex

unread,
Jan 26, 2012, 2:13:55 AM1/26/12
to nodejs
On Jan 26, 1:45 am, Phil Daws <ux...@splatnix.net> wrote:
> When I run node index.js I receive:
>
> node.js:218
> throw e; // process.nextTick error, or 'error' event on first tick
> ^
> ReferenceError: server is not defined

You haven't defined `server` used on line 9 of asterisk.js: var socket
= io.listen(server);

Phil Daws

unread,
Jan 26, 2012, 2:25:35 AM1/26/12
to nod...@googlegroups.com
Hi mscdex,

the problem is that the server variable is being defined in server.js as that is where the web server is created and the socket.io binds to the listening port. As I have split it into separate modules it is though I should somehow being making the variable global so that it can be referenced.
--
Thanks, Phil

> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

Phil Daws

unread,
Jan 26, 2012, 3:28:54 AM1/26/12
to nod...@googlegroups.com
Okay, I have changed the code around now so that index.js contains:

server.start(asterisk);

and then in server.js I changed the start function to be:

function start(asterisk) {
...
...
asterisk()
}

asterisk.js has then been updated to export itself as:

function asterisk() {
}
exports.asterisk = asterisk;

if I then run node index.js and the server does begin to start but when it hits the asterisk call I receive:

[uxbod@cyborg asterisk]$ node index.js
info - socket.io started
info - Web server has started on port 8080

node.js:218
throw e; // process.nextTick error, or 'error' event on first tick
^

TypeError: object is not a function
at Object.start (/home/uxbod/Development/nodejs/asterisk/server.js:33:5)
at Object.<anonymous> (/home/uxbod/Development/nodejs/asterisk/index.js:4:8)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:32)
at Function._load (module.js:310:12)
at Array.0 (module.js:484:10)
at EventEmitter._tickCallback (node.js:209:41)

I thought by passing asterisk as a function to the server then the socket would be available to the asterisk function ? Confused ?!?!? :(

mscdex

unread,
Jan 26, 2012, 4:06:42 AM1/26/12
to nodejs
On Jan 26, 3:28 am, Phil Daws <ux...@splatnix.net> wrote:
> Okay, I have changed the code around now so that index.js contains:
>
> server.start(asterisk);
>
> and then in server.js I changed the start function to be:
>
> function start(asterisk) {
>    ...
>    ...
>    asterisk()
>
> }
>
> asterisk.js has then been updated to export itself as:
>
> function asterisk() {}
>
> exports.asterisk = asterisk;
>

if index.js is simply passing the contents of require('./asterisk') to
server.start(), then you need to either: instead call
asterisk.asterisk() inside server.js, change index.js to instead do
server.start(asterisk.asterisk), or make asterisk.js do
`module.exports = asterisk;` instead of `exports.asterisk = asterisk;`

mscdex

unread,
Jan 26, 2012, 4:08:20 AM1/26/12
to nodejs
On Jan 26, 2:25 am, Phil Daws <ux...@splatnix.net> wrote:
> the problem is that the server variable is being defined in server.js as that is where the web server is created and the socket.io binds to the listening port. As I have split it into separate modules it is though I should somehow being making the variable global so that it can be referenced.

When modules are loaded, they are wrapped in a closure with no access
to outside variables.

Phil Daws

unread,
Jan 26, 2012, 5:32:35 AM1/26/12
to nod...@googlegroups.com
mscdex, appreciate your help but I must be tired I think as I cannot even get a simple app to work ie. http + socket.io; set up a webserver and write out a page that has a div element, and then push a piece of text from the app to that element ;(
--
Thanks, Phil

mscdex

unread,
Jan 26, 2012, 7:25:22 AM1/26/12
to nodejs
On Jan 26, 5:32 am, Phil Daws <ux...@splatnix.net> wrote:
> mscdex, appreciate your help but I must be tired I think as I cannot even get a simple app to work ie. http + socket.io; set up a webserver and write out a page that has a div element, and then push a piece of text from the app to that element ;(

The socket.io "how to use" page shows how to integrate the two pretty
easily (along with a client example): http://socket.io/#how-to-use

You should be able to quickly modify that example for your needs.

Phil Daws

unread,
Jan 26, 2012, 8:23:49 AM1/26/12
to nod...@googlegroups.com
mscdex,

I guess with it all being new it is taking a while to sync in :) I now have a server and a client that when I go to the web page and enter the URL it displays the data that has been emitted. The next step was to have the web page automatically update every few seconds so that new events would automatically update the div. This is the HTML I tried:

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script language="javascript">
var socket = io.connect("http://localhost");
var auto_refresh = setInterval( function() {
socket.on("event", function(data) {
$("#event_text").text(data.exten);
})
}, 2000);
</script>
</head>
<body>
<b>Some event happened=<div id="event_text"></div></b>
</body>
</html>

Now if I click on refresh it works fine; but I am wondering that when the setInterval is wrapper around the socket.on() it fails to contact the server side again.

How would you approach it as I don't want to refresh the whole page just the indivdual div element.
--
Thanks, Phil

mscdex

unread,
Jan 26, 2012, 12:32:19 PM1/26/12
to nodejs
On Jan 26, 8:23 am, Phil Daws <ux...@splatnix.net> wrote:
> mscdex,
>
> I guess with it all being new it is taking a while to sync in :) I now have a server and a client that when I go to the web page and enter the URL it displays the data that has been emitted.  The next step was to have the web page automatically update every few seconds so that new events would automatically update the div. This is the HTML I tried:
> [...]
> var auto_refresh = setInterval( function() {
> socket.on("event", function(data) {
>  $("#event_text").text(data.exten);})
> }, 2000);

You shouldn't do setInterval here. All you need to do is set the
handler once and the 'event' event handler will be executed every time
a 'event' is emitted on the server.

If your asterisk.js is similar to what it initially was, then you're
not emitting events correctly. Try this instead:

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

// .....

event.on('peerstatus', function(data) {
io.sockets.emit('event', data);
});

This will emit an event called 'event' on the client-side, with the
data object sent as the only argument. So on the client side you can
then properly access data.status, data.extlen, etc from your event
handler.

Marty Nelson

unread,
Jan 26, 2012, 12:47:30 PM1/26/12
to nod...@googlegroups.com
socket.io is a push technology, you shouldn't be wrapping it with setInterval! (that's a polling technique).

If set up correctly, the defined function will be called every time the client receives a message from the server.

Phil Daws

unread,
Jan 27, 2012, 3:57:18 AM1/27/12
to nod...@googlegroups.com
Thank you mscdex and Marty for your help. Finally got my head around it and have my first app with nodejs, socket.io, and express running :) Absolutely love nodejs! Now just need to start learning more JavaScript and exploring the power of this project.
--
Thanks, Phil

Reply all
Reply to author
Forward
0 new messages