0

I have an application server and client with socket.io, and I'm trying to have a communication between them. Unfortunetely it's not working as expected. In general the process is like.

1 - Initiate server. 2 - Initiate client 3 - Connect client in the server 4 - server receive the message that connect and send a broadcast to client. 5 - client receive message and send back "hello" message and present in the console

step 4 and 5 not working at all. any help?

Codes:

SERVER -----------------------------------------------

const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {
  console.log('a user connected')
  io.emit('broadcast msg from server')


   io.on('hello', () => {
    console.log('mensagem do client')
    io.emit('hello');
    });

});

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

//Client -----------------------------------------------

const ioClient = io.connect("http://localhost:3000")
const readline = require('readline')

ioClient.on("connect", () => {
    console.log('conectou')
});

const rl = readline.createInterface({
    input: process.stdin,   
    utput: process.stdout
});

ioClient.on("broadcast msg from server", () => {
    console.log("broadcast msg from server");

    ioClient.emit("hello");
    
});

ioClient.on("hello", () => {
    console.log("helloFrom server");
});
Anatoly
  • 20,799
  • 3
  • 28
  • 42
Bruno Pimenta
  • 19
  • 1
  • 4

2 Answers2

0

I believe you are missing out on the client-side socket.io-client as the server-side socket.io will not work on the client side. You'll need to install it first before establishing the connection to the server side.

$ npm install socket.io-client

After installing the above, you will have to insert the following line before the first line of Client.

const io = require('socket.io-client');
kenmistry
  • 1,934
  • 1
  • 15
  • 24
0

I see three problems in the code you post:

  1. You don't show importing the Express module on the server
  2. You don't show importing the socket.io-client module in the client
  3. You code for listening for the "hello" message on the server is wrong.

When I run this exact code (filling in some modules you don't show code to load):

Server:

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {
    console.log('a user connected')
    io.emit('broadcast msg from server')


    io.on('hello', () => {
        console.log('mensagem do client')
        io.emit('hello');
    });

});

server.listen(3000, () => {
    console.log('listening on *:3000');

});

Client:

const io = require('socket.io-client');
const ioClient = io.connect("http://localhost:3000")

ioClient.on("connect", () => {
    console.log('conectou')
});

ioClient.on("broadcast msg from server", () => {
    console.log("broadcast msg from server");

    ioClient.emit("hello");

});

ioClient.on("hello", () => {
    console.log("helloFrom server");
});

I get this output in the client console:

conectou
broadcast msg from server

And this output in the server console:

listening on *:3000
a user connected

So, it appears that the client is indeed getting a message. So, apparently something is different in your actual code than what I'm running here if your client isn't getting that message. The main thing you don't show in your code is this in the client:

const io = require('socket.io-client');

and this in the server:

const express = require('express');

Note also that this part of your code is wrong:

io.on('hello', () => {
    console.log('mensagem do client')
    io.emit('hello');
});

On the server messages arrive to a socket, not to io. io events are for connection mechanics only.

So, on your server, this should be:

socket.on('hello', () => {
    console.log('mensagem do client')
    io.emit('hello');
});

After making that additional fix, the client console shows:

conectou
broadcast msg from server
helloFrom server

And the server console shows:

listening on *:3000
a user connected
mensagem do client
jfriend00
  • 683,504
  • 96
  • 985
  • 979