Iman Salehi
Iman Salehi

Reputation: 956

nothing happens when i emit some value to socket server

I have an expressjs server code which listen to a socket port like what you see below.

websocket.on('connection', (socket) => {
  console.log('A client just joined on', socket.id);
  websocket.on('messageIM', (message) => {
     console.log('msg:'+message)

});
});

my client side application is a react-native mobile project which contains a code like this:

  constructor(props) {
super(props); 
this.socket = SocketIOClient('http://192.168.140.51:3000/');

  }

and i get client connect 'A client just joined on ***' log on server console, means the connection to the server is successfully . but when i emit some variable .... nothing will happens in server side!...

call()=>{
 this.socket.emit('messageIM','Hi server')
}

means when i call above call() function nothing happens in server.

this are the codes i'v written in both client and server side please give some help if you have some experience with that .

Upvotes: 0

Views: 121

Answers (1)

Richard Ayotte
Richard Ayotte

Reputation: 5080

Looks like you used the wrong variable to listen on. The connection event returns the socket which is what you want to use. Rename websocket.on('messageIM' to socket.on('messageIM'

websocket.on('connection', socket => {
  console.log('A client just joined on', socket.id);
  socket.on('messageIM', message => {
     console.log(`msg: ${message}`)
  });
});

Upvotes: 1

Related Questions