1

I'm unable to perform chat functionality in my application. I have created a port 8000 on which my applicattion runs. I'm trying to implement the socket.io module inside my application. For this i have created another file named as chat.js. Firstly I define socket as socket= io(). Then it was showing error as

//GET /socket.io/?EIO=3&transport=polling&t=NAO4VZg 404 6.362 ms - 1908"

Then after lots of searches i found the replacement for the above statement as

var socket = require('socket.io-client')('http://localhost:8000/users/chatter', { transports: ['websocket'] })

Now running the code with this statement. The below error is displaying:

chat.js:1 Uncaught ReferenceError: require is not defined
  at chat.js:1

Here is the screenshot of error: Error Screenshot

After lots of searches i found the replacement for the above statement as

var socket = require('socket.io-client')('http://localhost:8000/users/chatter', { transports: ['websocket'] })
var message = document.getElementById('message'),
      handle = document.getElementById('handle'),
      btn = document.getElementById('send'),
      output = document.getElementById('output'),
      feedback = document.getElementById('feedback');

// Emit events
btn.addEventListener('click', function(){
    socket.emit('chat', {
        message: message.value,
        handle: handle.value
    });
    message.value = "";
});

message.addEventListener('keypress', function(){
    socket.emit('typing', handle.value);
})

// Listen for events
socket.once('connect', socketConn => {
socket.on('chat', function(data){
    feedback.innerHTML = '';
    output.innerHTML += '<p><strong>' + data.handle + ': </strong>' + data.message + '</p>';
});

socket.on('typing', function(data){
    feedback.innerHTML = '<p><em>' + data + ' is typing a message...</em></p>';
});
}); 

Now running the code with this statement. The below error is displaying:

chat.js:1 Uncaught ReferenceError: require is not defined
  at chat.js:1
Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
  • You cannot use `require` in client-side (browser) JavaScript. You need to use a ` –  Jun 09 '20 at 10:03
  • Does this answer your question? [Client on node: Uncaught ReferenceError: require is not defined](https://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined) –  Jun 09 '20 at 10:16
  • then how could i use it ? – Chitranshi Saxena Jun 09 '20 at 10:18
  • I assume you initially tried this? https://socket.io/docs/#Using-with-Express –  Jun 09 '20 at 10:28
  • Have you looked at the docs? https://github.com/socketio/socket.io-client. You **cannot** user `require` in the browser - that's a `CommonJS` thing that browsers don't support, so unless you have a build system in place, `require` won't work. – goto Jun 09 '20 at 10:35
  • on your frontend just use socket.io-client.js which you can find https://cdnjs.com/libraries/socket.io and then inside your script tag's just create `var socket = io(options)`; ofc give options if needed =) – halilcakar Jun 09 '20 at 18:41
  • how can i use socket.io-client.js – Chitranshi Saxena Jun 11 '20 at 05:30
  • It's the first code snippet in that `GitHub` repo I linked. – goto Jun 11 '20 at 11:27
  • Thanks to all of your for response. I sorted out my problem. – Chitranshi Saxena Jun 17 '20 at 09:01

0 Answers0