I am trying to use io.socket onto a node js application.
In server side:
io.on('connection', function(client){
client.on('event', function(data){console.log("I am here")});
client.on('disconnect', function(){});
});
In client side:
var socket = io.connect();
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
server.listen('80');
app.listen(port);
But I think I have some problem with the port because the port is defined under folder bin/www.
when i try to upload the code into heroku, the heroku app is broken down. It was working fine in localhost.
Related
I have nodeJs application which is running on server http://mysite.co:8081. Where I have chat application. Using socket.io for communication.
Now I want to connect to socket from localhost -- I am trying to connect from my machine where I have wamp installed and in that created one html file in www/test/index.html.
The index.html have below code only.
<script src="http://mysite.co:8081/socket.io/socket.io.js"></script>
<script>
var socket = io('http://mysite.co:8081');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
The socket.io.js file is loading correctly, But I am getting below error
Uncaught TypeError: io is not a function
Let me now what I am doing wrong.
Thanks,
try this one:
<script src="http://mysite.co:8081/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://mysite.co:8081');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
I'm trying to use this simple tutorial:
http://socket.io/socket-io-with-apache-cordova/
My node.js is working fine and i'm emulating to iOS without problem, but the socket.io isn't working, here is my javascript(the same way as the tutorial above):
app.initialize();
document.addEventListener('deviceready', function() {
console.log(socket);
socket.on('connect', function() {
socket.on('text', function(text) {
alert(text);
});
});
});
and one more thing how can i get this console.log to debug?
here is how i'm getting socket.io(the same way as the tutorial above):
<script type="text/javascript" src="http://cdn.socket.io/socket.io-1.0.3.js"></script>
here is my server.js(the same way as the tutorial above):
var server = require('http').createServer();
var io = require('socket.io')(server);
io.sockets.on('connection', function (socket) {
console.log('socket connected');
socket.on('disconnect', function () {
console.log('socket disconnected');
});
socket.emit('text', 'wow. such event. very real time.');
});
server.listen(3000);
I think, that the problem and the tutorial didn't told is how i would connect my cordova app with port 3000
I made it, this tutorial is very good but it's not totally right.
You have to connect the socket to your server first (i'm using localhost and port 3000, but if you're using some server outside, i think you've to just put the ip and port):
var socket = io.connect('http://localhost:3000');
and after that, you call "socket.io", here is my complete code:
document.addEventListener('deviceready', function() {
var socket = io.connect('http://localhost:3000');
socket.on('connect', function() {
socket.on('text', function(text) {
alert(text);
});
});
});
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
var socketHost = "http://localhost:3000";
var socket = io.connect(socketHost);
I know this error message has been asked in lots of question but I haven't found one that matches my situation. Below I show you the server (node.js) and the client code. The socket.io.js file is included and definitely present.
This problem occurs if you are not listening to the correct port.
Server
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Client
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:80');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
If the server and the client is not listening to the same port, then you will not be able to load the socket.io library and io will be undefined. I'm guessing that you did not specify the port on the client when connecting to a different domain. If you are using express, the port number should be the port Express listens to by default.
I've set up a Ubuntu 10.04 server in my home network. I installed a lamp-server, NodeJs and Socket.io on it. It al seems to work fine but I can not make a connection from my client to the server. I think that I am missing the point of where to store the client files and/or how to the IP adress and port should be put in the code.
I'm using the example from David Walsh (http://davidwalsh.name/websocket). My ip-adress of the server is 192.168.1.113. On the client I store app.js in
/usr/local/bin
That is where node is installed as wel.
Server code
// Require HTTP module (to start server) and Socket.IO
var http = require('http'), io = require('socket.io');
// Start the server at port 8000
var server = http.createServer(function(req, res){
// Send HTML headers and message
res.writeHead(200,{ 'Content-Type': 'text/html' });
res.end('<h1>Hello Socket Lover!</h1>');
});
server.listen(8000);
// Create a Socket.IO instance, passing it our server
var socket = io.listen(server);
// Add a connect listener
socket.on('connection', function(client){
// Success! Now listen to messages to be received
client.on('message',function(event){
console.log('Received message from client!',event);
});
client.on('disconnect',function(){
clearInterval(interval);
console.log('Server has disconnected');
});
});
When I start the server with Node and enter the adress in a browser (http://192.168.1.113:8000) I see 'Hello Socket Lover!' so I guess this works fine.
I store my client file (client.html) in
/var/www
That is where the home folder of my LAMP server is.
Client.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script>
// Create SocketIO instance, connect
var socket = new io.Socket('http://192.168.1.113',{
port: 8000
});
socket.connect();
// Add a connect listener
socket.on('connect',function() {
console.log('Client has connected to the server!');
});
// Add a connect listener
socket.on('message',function(data) {
console.log('Received a message from the server!',data);
});
// Add a disconnect listener
socket.on('disconnect',function() {
console.log('The client has disconnected!');
});
// Sends a message to the server via sockets
function sendMessageToServer(message) {
socket.send(message);
}
</body>
</html>
Now when I open this file in a browser (http://192.168.1.113/client.html) nothing happens. I see no connection on my Ubuntu server or other logs and there are no messages in the browser.
Ehmm... what am I doing wrong? I've tried for hours, changing all kind of things but still no result.
With the help of pmavik (see comments) my question is answered.
What I didn't know was how to serve the client file properly (index.html). I copied an example from www.socket.io, added the right ip-adress and port and now a connection is made.
The server file (app.js) and the client file (index.html) should be in the directory where Node is installed. The Node-server sends the client file to the browser so there is no need to put the client file in the /var/www directory where the 'normal' files for websites are.
app.js
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8000);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
index.html
<html>
<head></head>
<body>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://192.168.1.113:8000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</body>
</html>
I am using socket.io in nodejs and I am able to send data from client to server. But when I emit from server, the client does not seem to be receiving this...
What am i missing ?
server:
socket = io.listen(app);
socket.sockets.on('connection', function(client){
client.on('something-from-client', function(msg){
console.log(msg);
//do something.
client.emit('some-result',{"total":docs.length});
});
});
client:
var socket = io.connect('http://localhost:9999');
socket.on('some-result', function(data){
console.log('received from server', data);
});
socket.emit("something-from-client", {"lat":lat, "lng":lng});
Ok got it working, if express is being used, some minor changes need to be done on server side
var port = 8111;
var server = express.createServer();
io = require('socket.io').listen(server);
server.listen(port);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
As promised - Working code - https://github.com/parj/node-websocket-demo/tree/socket_emit
CF Reference - http://socket.io/#how-to-use