jquery - Simple chat application not working on live website -
i'am new nodejs. following tutorial online on how make socket.io(chat application). made simple application works offline, in order work, have go command prompt, go project , type : node server : start sever on port 3000.
i wanted put small application onto website, did here
but when try type in chat box wont display, , giving me bunch of errors in console. know has port. don't know how make work on live website.
here server side code:
// include of our modules var express = require('express'); // set variable "app" = 'express' variable var app = express(); // set our server variable, , pass in 'app' variable var server = require('http').createserver(app); // include socket.io , listen 'server' variable var io = require('socket.io').listen(server); // * going have 2 arrays // 1 users, other 1 connections users = []; connections = []; // run server, , listen assigned port server.listen(process.env.port || 3000); console.log('server running...'); // create route, function going take in "request" , "response" app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); // open connection socket.io io.sockets.on('connection', function(socket) { connections.push(socket); console.log('connected: %s sockets connected', connections.length); // write our disconnect, , console.log how many still connected socket.on('disconnect', function (data) { connections.splice(connections.indexof(socket), 1); console.log('disconnected: %s sockets connected', connections.length); }); // send message socket.on('send message', function (data) { // emmit new message event io.sockets.emit('new message', {msg: data}); }) });
here client side code:
<div class="container"> <div class="row"> <div class="col-md-4"> <div class="well"> <h4>online users</h4> <ul class="list-group" id="users"></ul> </div> </div> <div class="col-md-8"> <div class="chat" id="chat"></div> <form id="messageform"> <div class="form-group"> <label>enter message</label> <textarea class="form-control" id="message"></textarea> <br /> <input type="submit" class="btn btn-primary" value="send message"> </div> </form> </div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.6/socket.io.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <script> $(function() { var socket = io.connect(); var $messageform = $('#messageform'); var $message = $('#message'); var $chat = $('#chat'); // create event when form submitted $messageform.submit(function(e) { e.preventdefault(); socket.emit('send message', $message.val()); $message.val(''); }); socket.on('new message', function(data) { $chat.append('<div class="well">'+data.msg+'</div>'); }); }); </script>
socket.io auto-configures connection when ommit connection url
to precise pass url io.connect (more info)
var socket = io.connect('http://<website_name>:<port>/');
like example in case:
var socket = io.connect('http://davidtrushkov.com:3000/');