demathieu demathieu - 1 year ago 238
Javascript Question

socket IO send form data from client to server

I'm trying to send a simple message from a form to my server using Sadly enough this is failing, I know the client has a connection with the server, but it does not appear to receive the messages.

Could someone explain to me why my code is failing?


var app = require('express')();
var http = require('http').Server(app);
var io = require('')(http);

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');

http.listen(8001, function(){
console.log('listening on *:8001');

io.on('connection', function(socket){
console.log('connection is er');
socket.on('message', function(msg){
console.log('message: ' + msg);

index.html :

<script src=""></script>
<script src=""></script>
var socket = io();
socket.emit('message', $('#Input').val());
//socket.emit('message', "Input");
return false;
<form id = "form" action = "">
Input: <input type="text" id="Input"><br>
<input type="submit" value="Submit">

Answer Source

It looks like your client is using JS file from website but you didn't specified host and port of YOUR server.

Check out the documentation you need to do something like

var socket = io.connect('http://localhost:8001');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download