Bula
Bula

Reputation: 2792

Simple server-client socket io

I'm trying to set up a simple server/client architecture using socket.io.

I have the following code in my main.js

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);


app.get('/', (req, res) => {
  res.send('<h1>Hello world</h1>');
});

io.on('connection', function(socket){
    console.log('a user connected')
})

http.listen(3001, () => {
  console.log('listening on *:3001');
});

I have the following code in my client.js

const io = require("socket.io-client");
let socket = io(':3001')

I then run node main.js which prints "listening on *:3001", however when I run node client.js the expected "a user connected" is not printed. What am I missing in this simple architecture?

Upvotes: 0

Views: 49

Answers (1)

SINCOS
SINCOS

Reputation: 89

It should work if you write it like this in your client.js:

let socket = io('http://localhost:3001/')

Upvotes: 1

Related Questions