Cristobal Wetzig
Cristobal Wetzig

Reputation: 381

node.js websocket server to html

I was wondering if you could help me with a conundrum. I'm new to node.js and would appreciate some help.

I have a simple html (client) page using jquery and socket.io.js a external server. the client sends events to a node.js app used as a websocket server, and this works well and as expected.

However I am clueless on how to do write the incomming events to something more useful than the console.log in the node.js app.

At the moment it could not be simpler.

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

io.set('log level',1);

io.sockets.on('connection', function (socket) {  
    socket.on('clientevent', function (message) {
        console.log(message);
    });
    socket.on('disconnect', function () {
        console.log('disconnected');
    });
});

I want to be able to write events from the client.html to some piece of html (DOM) from the node.js app. I want to do this with something like jquery's $('#event').append(message), and also be able to talk to the node.js server trough something like $('#someid').click...

Or am I going about this completely wrong?

Upvotes: 0

Views: 1543

Answers (1)

patalmypal
patalmypal

Reputation: 6712

The basic example is perfect. Socket.io example with Jquery and DOM manipulation gives you a more elaborate example using Jquery and DOM manipulation. You'll have to upload client.html to a web-server, run app.js from node and then access client.html.

Socket.io listens on port 8000 and the client has already been configured to access it at that port. Specifically refer the code for DOM manipulation using Jquery.

Hope this helps.

Upvotes: 1

Related Questions