There is a function:

$('#button').click(function(){var val=$('#area').val();socket.emit('message',{val:val});}); 

There is a socket:

 var socket = io.connect('http://site.com'); socket.on('message',function(data){send(data.val);}); 

And there is a function:

 function send(val){$('#place').append(val);} 

All necessary js-files are connected, but nothing works. Question: what should be on site.com that all this worked?

  • which thread is a web server server, if php, then for example phpdaemon. - zb '

1 answer 1

And where is the server part? All that you have brought is just the front-end, but you also need a back-end :)

At a minimum, you should have a Node.js server on your server + a socket.io module and the application script itself:

server.js

 var io = require('socket.io').listen(1234); io.sockets.on('connection', function (socket) { socket.on('message', function(obj) { // какие-то действия socket.emit('message', obj); } } 

It also needs to be run: node server.js and restarted with each change, so that they take effect

It seems that you do not understand how the back-end and the front-end should interact. Let's sort your code:

This piece of code handles a click on the button and accesses the message event on the server:

 $('#button').click(function(){var val=$('#area').val();socket.emit('message',{val:val});}); 

Here in the first line you are trying to connect to the server and establish a socket connection, and on the second line you create a message handler from the server, where the only parameter is data:

 // Обязательно укажите номер порта var socket = io.connect('http://site.com:1234'); socket.on('message',function(data){send(data.val);}); 

And this is the function that will be executed when the server sends a message (in the context of your code):

 function send(val){$('#place').append(val);} 

Proceeding from all this, it is obvious that you do not have a server part that would handle the message event and send a message (do not repeat the names of events and messages in the future so as not to be confused). Above, I gave the code that will create a socket server and hang it on port 1234. Next comes the description of the message event, which you will access from the client, sending the {val:val} object as a parameter:

 $('#button').click(function(){var val=$('#area').val();socket.emit('message',{val:val});});