0
//backend code
var express = require("express");
var app = express();
var http = require('http');

var startServer=http.createServer(app);
var socketIO = require('socket.io').listen(startServer);
startServer.listen(8080, function () {
    console.log('server running on',8080)
});

socketIO.on('connection', function (socket) {
    console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>connected')
    var userSocketObject = {};
    userSocketObject.socket_id = socket.id;
    socket.emit('news', { hello: 'world' });
        socket.on('my other event', function (data) {
        console.log(data);
    });
});

//index.html code angularjs
var socket = io.connect('http://127.0.0.1:8080');
socket.on('news', function(data){
    console.log(data);
    socket.emit('my other event', {my: 'data'});
});

I'm trying to connect but I'm getting an error in the browser:

GET http://127.0.0.1:8080/socket.io/?EIO=3&transport=polling&t=LV9VGzC" Error (404): "Not found"
sahil
  • 39
  • 1
  • 8

2 Answers2

1

i have resolved the problem , the problem was in directory path app.use(express.static(path.join('public')))

sahil
  • 39
  • 1
  • 8
0

Try changing your backend code into

var socketio = require('socket.io')(startServer, {
  path: '/socket.io-client'
});

socketio.on('connection', function (socket) {
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

And on the clientside, the easiest way is to follow the instructions given in this repo: https://github.com/btford/angular-socket-io

palaska
  • 11
  • 2