0

This is a super simple question but i am really new to Node, i'm reading all the beginners guide now so no need to direct me to them! guides tell you how to use node not how to get the thing working!

I'm finding node.js tutorials assume way too much knowledge and set up.

i have my app.js file which contains:

SERVER SIDE

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

// New Code

var mongo = require('mongojs');
var db = mongo('site');
var app = express();

// all environments
app.set('port', process.env.PORT || 80);
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
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('/libraries/*', function(req, res){
    req.url.replace('/libraries', ''); // remove libraries prefix
    var path = req.url.split("/libraries").pop();
    console.log(path);
    switch (path) {
        case '/socket.io/socket.io.js':
            res.sendfile('node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js');
            break;
    }

});

// setup server
var server = http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

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

CLIENT SIDE:

var socket = io.connect();
socket.on('ready', function (data) {
    marketData = data; // raw data from the DB 
});

All good and well.

I want to console log 'test' into the website console.

My assumption is i define a function server side and call it client side... How on earth do I do this?!?!

I just want them to talk to each other.

Server side i would like to define a var and call it client side.

My assumption:

Server side:

function test () {
console.log ('test');
}

Client side:

test();

Console on my website says: 'test'.

5
  • What you've got so far is a socket connection between client and server. If that's what you want to use for the communication, then you need to define a "protocol" between the two so that you can send a command from the client and the server will parse what was sent and then know what to do. There is no default protocol. So basically, you need code on the server that examines what the client sent and decides what to do based on what they sent. Commented Apr 5, 2014 at 13:03
  • Hi jfriend00. Thanks for the response. This slightly touches on my point of assumed knowledge. I'm not sure how to define a 'protocol' or how to or what parsing is? Currently i'm not looking for anything to examine anything the client sent, i just want to have the client emit a function (console.log('test')) and for my client side javascript to be able to run that function. It's really about that first step of getting the client and server talking. Thanks for the response :) Apologies, i have been struggling with node for weeks and i'm lost and just want a foothold. Commented Apr 5, 2014 at 13:16
  • 1
    The simplest example here is that you send some text from client to server. You receive that data on the sever and you look at what the client sent. If it sent "x", then you trigger action "y". In your case, if it sent "test", then you call test(). As far as I know, there's no remote procedure calling functionality built into web sockets like you seem to be looking for. You have to write your own primitive one which just examines what the client sent and then decide what to do based on what it sent. It's nothing more than a few if statements on the server and send data on the client. Commented Apr 5, 2014 at 13:21
  • 1
    Look at the examples: danielnill.com/nodejs-tutorial-with-socketio and chrislarson.me/blog/… and vijayannadi.wordpress.com/tutorials/… and code.tutsplus.com/tutorials/… Commented Apr 5, 2014 at 13:25
  • Thanks, i'll read through these links, they seems to be getting closer to what i'm after, thanks for the time Commented Apr 5, 2014 at 13:30

2 Answers 2

1

Expeting that you want to do communications with Socket.io, add to the bottom of your server script:

io.sockets.on('connection', function (socket) {
    // Do what you need to do here and finally emit "read"
    // here setTimeout simulates your database query or similar
    // process of building the data you want to send to the client
    setTimeout(function (data) {
        socket.emit('ready', data);
    }, 1000);
});

then at the client side have:

var socket = io.connect();
socket.on('ready', function (data) {
    marketData = data; // raw data from the DB 
    test();
});

When your client connects, the server will run your business logics and send back the data by emitting "ready" event to the client. I added call for test() in your client event handler that might help on understanding the process.

Sign up to request clarification or add additional context in comments.

Comments

0

To get a connection between your javascript (client side) and the server the below should work:

Client side:

socket.on('news', function (data) {
  console.log(data);
});

Server side:

io.sockets.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
});

Will console log your data which is an object { hello: 'world' }

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.