Howie
Howie

Reputation: 2778

Do I need socket.io-client on browser-side of things?

EDIT: The answer appears to be no.

I'm new to Node.js, bower and Socket.IO and I'm not sure what I need for my purpose.

I'm making an app that has a frontend (where browsers connect) and a backend (a single Node.js server).

What do I need to create a Socket.IO server instance on the backend? What do I need on the client-side? Does the Socket.IO package contain both?

Upvotes: 0

Views: 665

Answers (1)

Arunkumar Vasudevan
Arunkumar Vasudevan

Reputation: 5330

First install socket.io using below command

npm install socket.io

and the call socket.io in your server js File

var io = require('socket.io');

And the create connection in your server js file

var app = express();
app.get('/', function(req, res){
fs.readFile('index.html', function(Error,data){
res.writeHead(200,{'Content-Type':'text/html'});
res.write(data);
res.end();
});
});
server = http.createServer(app);
var IO = io.listen(server);
server.listen(3000);
IO.sockets.on('connection', function(socket) {
socket.on('msg_to_server', function(data) {
  console.log(data);
        });
        });

Add this script inside head Tag in your index.html

<script src="/socket.io/socket.io.js"></script>

in your index.html create socketio connection

var socketio = io.connect("127.0.0.1:3000");

send some data to server following way

socketio.emit('msg_to_server',{ message : 'some data' });

Upvotes: 1

Related Questions