Note that there are some explanatory texts on larger screens.

plurals
  1. PONode.js and Socket.io not working on this Example
    primarykey
    data
    text
    <p>Can you please take a look at following code and let me know what I am doing wrong on that? Running Node.js + Express + Socket.io on Ubuntu 12.4 I am trying to create a simple Push function. The server is working perfectly but I cant see the div id "status" content on the page and apparently the function is not running! </p> <p>Thanks for your help and comment in advance and sorry for long post!<br /></p> <p>Here is the <strong>app.js</strong> file /** * Module dependencies. */</p> <pre><code> var express = require('express') , routes = require('./routes') , user = require('./routes/user') , http = require('http') , path = require('path'); var app = express(); // all environments app.set('port', process.env.PORT || 3000); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(path.join(__dirname, 'public'))); // development only if ('development' == app.get('env')) { app.use(express.errorHandler()); } app.get('/', routes.index); app.get('/users', user.list); var status = "I am not changed yet!."; io.sockets.on('connection', function (socket) { io.sockets.emit('status', { status: status }); socket.on('reset', function (data) { status = "You Change the text!"; io.sockets.emit('status', { status: status }); }); http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port')); }); </code></pre> <p>I modified the "<strong>package.json</strong>" as below, adding the sockt.io to dependencies</p> <pre><code>{ "name": "application-name", "version": "0.0.1", "private": true, "scripts": { "start": "node app.js" }, "dependencies": { "express": "3.2.3", "jade": "*", "socket.io:0.9.14" } } </code></pre> <p>in the <strong>view</strong> folder and in <strong>index.jade</strong> I have</p> <pre><code>&lt;div id="status"&gt;&lt;/div&gt; &lt;button id="reset"&gt;Reset!&lt;/button&gt; </code></pre> <p>and in <strong>layout.jade</strong> What I have is:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Title&lt;/title&gt; &lt;meta name="description" content=""&gt; &lt;meta name="author" content=""&gt; &lt;!-- HTML5 shim, for IE6-8 support of HTML elements --&gt; &lt;!--[if lt IE 9]&gt; &lt;script src="http://html5shim.googlecode.com/svn/trunk/html5.js"&gt;&lt;/script&gt; &lt;![endif]--&gt; &lt;!-- styles --&gt; &lt;link href="/css/main.css" rel="stylesheet"&gt; &lt;/head&gt; &lt;body&gt; &lt;%- body %&gt; &lt;script src="node_modules/socket.io/socket.io.js"&gt;&lt;/script&gt; &lt;script src="public/js/libs/jquery.js"&gt;&lt;/script&gt; &lt;script src="public/js/main.js"&gt;&lt;/script&gt; &lt;/body&gt; </code></pre> <p></p> <p>and Finally this the function which I have in <strong>main.js</strong>:</p> <pre><code>var socket = io.connect(window.location.hostname); socket.on('status', function (data) { $('#status').html(data.status); }); $('#reset').click(function() { socket.emit('reset'); }); </code></pre> <p>error Message after editing by robert Hint</p> <pre><code>SyntaxError: Unexpected end of input at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:901:3 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload