Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket with node js example

I have a problem.

Client side code

 <html>
    <body onload="fun()">
    <script src="C:\cygwin\usr\local\lib\node\.npm\socket.io\0.6.16\package\support\socket.io-client\socket.io.js"></script> 
    <script> 

    function fun()
    {
     alert("hello")   
     var socket = new io.Socket('localhost',{'port':8090});

    socket.on('connect', function(){ 
      socket.send('hi!'); 
    }) 

     socket.on('connect', function(){ console.log('connected to server');socket.send('hi there, this is a test message'); })


    socket.on('message', function(data){ 
      alert(data);
    })
    socket.on('disconnect', function(){}) 
    }
    </script> 
    </body>
    </html>

server side code:

var http = require('http'),  
    io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io') 

server = http.createServer(function(req, res){ 
 // your normal server code 
 res.writeHead(200, {'Content-Type': 'text/html'}); 
 res.end('<h1>Hello world</h1>'); 
});
server.listen(8090);

// socket.io 
var socket = io.listen(server); 
socket.on('connection', function(client){ 
  // new client is here! 
  client.on('message', function(){ console.log('message arrive'); }) 

  client.on('disconnect', function(){ console.log('connection closed');})

});

found this example from socket.io. When I run the server it gives me Socket io is ready. Accepting connection when I run the browser it is not showing anything and also on the firefox firebug console please help me to solve this problem.

like image 804
Abdul Rauf Avatar asked Mar 30 '11 06:03

Abdul Rauf


People also ask

How do I use a Socket in node JS?

// make a connection with the user from server side io. on('connection', (socket)=>{ console. log('New user connected'); }); Similarly, from the client-side, we need to add a script file and then make a connection to a server through which users send data to a server.

How do I connect Socket.IO to node JS?

In order to do it, you need to create an index. js file and install socket.io and express. You can use the following command: touch index. js && npm install express socket.io && npm install --save-dev nodemon .

Does Socket.IO require node JS?

In this guide we'll create a basic chat application. It requires almost no basic prior knowledge of Node. JS or Socket.IO, so it's ideal for users of all knowledge levels.

What is Socket.IO used for in the context of node JS?

Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. It is built on top of the WebSocket protocol and provides additional guarantees like fallback to HTTP long-polling or automatic reconnection.


1 Answers

You never call socket.connect() on the client side so the socket never tryes to connect to the server. Check the following code :

Client side ->

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="socket.io.js"></script> 
<script> 

function fun()
{
    var socket = new io.Socket('localhost',{'port':8090});

    socket.connect();

    socket.on('connect', function(){
        console.log('connected');
        socket.send('hi!'); 
    });


    socket.on('message', function(data){ 
        console.log('message recived: ' + data);
    });

    socket.on('disconnect', function(){
        console.log('disconected');
    });
}

$(document).ready(function() {
    fun();
});
</script> 
</head>
<body>
</body>
</html>

Server side ->

var http = require('http'),  
    io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io') 

server = http.createServer(function(req, res){ 
    // your normal server code 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end('<h1>Hello world</h1>'); 
});
server.listen(8090);

// socket.io 
var socket = io.listen(server); 
socket.on('connection', function(client){ 
    // new client is here! 
    client.on('message', function(){ 
        console.log('message arrive');
        client.send('some message');
    });

    client.on('disconnect', function(){
        console.log('connection closed');
    });

});
like image 168
Poelinca Dorin Avatar answered Nov 15 '22 04:11

Poelinca Dorin