B
B
BarneyGumble2018-04-12 12:37:00
Node.js
BarneyGumble, 2018-04-12 12:37:00

How to make an Express application and socket.io work on different ports?

Good afternoon
Client:

var socket = io('http://localhost:3000');
socket.emit("chat message", 665, 500, 500);

socket.on("chat message", function(movingEmail, movingX, movingY) {
  console.log(movingEmail + ' ' + movingX + ' ' + movingY);
});

Server (Express 4):
var app = require('express')();
var express = require('express');
var exphbs = require('express-handlebars');
var path = require('path');
var http = require('http').Server(app);

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

app.set('views', path.join(__dirname, 'views'));
app.engine('hbs', exphbs({defaultLayout:'layout'}));
app.set('view engine', 'hbs');

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req, res) {
    res.render('system', {layout: false});
});

io.on('connection', function(socket){
    socket.on('chat message', function(msg){
        console.log('message: ' + msg);
        socket.emit('chat message', msg);
    });
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});

Everything works perfectly.
As soon as I change the line on the client to And on the server to That, everything stops working and I start seeing an error in the browser console: socket.io-1.4.5.js:1 GET localhost:3000/socket.io/?EIO=3&transport= polling&... 404 (Not Found ) socket.io wants to run on an application port. How can I make the application and sockets run on different ports?
var socket = io('http://localhost:3001');
var io = require('socket.io')(3001);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Abcdefgk, 2018-04-12
@Abcdefgk

var io = require('socket.io')(3001); //любая цифра

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question