0

I've followed a tutorial to create a chat with Node.js and Socket.io. The point is that I want to modify this chat to obtain the following result:

I'd like to have two browser windows:

  • Window A: I can type and send everything I want.
  • Window B: I can't type, I can just view what everyone have typed (me included).

The point is: How can I send a message from Window A (hosted on localhost:8080) and display it inside Window B (hosted on localhost:8081)? Can someone give me a clear example on how to do that? Sorry if the question is a little bit confused but I'm kinda new to socket.io and the all the stuff listed in the tutorial that I've followed.

1 Answers1

0

You say that you already have figured out how to send message between Window A and Window B. Then you should split the code into 2 applications, "read and write" and "read mode", you only have to change the HTML code to test it.

The simplest solution would be to just modify the HTML in the "reader" windows. remove the form chat and there won't be any option for them to write in the chat including you. Of course this is possible to bypass but no security aspect is said to be taken into consideration.

<form id="chat">
   <input type="text" id="msg_text" name="msg_text" />
   <input type="submit" value="Send!" /> 
</form>
Fredrik
  • 484
  • 4
  • 13
  • Editing the HTML is just a piece of the puzzle I think. I've tried to set "io.connect('http://localhost:8080')" but this doesn't work because the message is not displayed ('null' is always displayed) –  Jun 12 '20 at 15:33
  • I presumed you already had solved the issue with communication between the ports. From your above comment, not the case. The tutorial are hosting the server on one port and then the clients (webbrowsers") are connecting to it. Thus, it is not about how to send message between different servers as what you are trying to do. The tutorial is using a central server where you are connecting the different clients and those should all use the same port number otherwise you get null. What you want is to create sub domains as: localhost:3000/read and localhost:3000/readAndWrite... – Fredrik Jun 15 '20 at 08:52
  • Well I know all the stuff you just told me about the tutorial. The point is that I want to modify it in order to obtain the communication between A and B. Is doing it with two different endpoints of just one server the only way to achieve that? –  Jun 15 '20 at 09:20
  • It sounds you want one server to be "as a client". It is described how to make one server listen to another server here: [socket-io-server-to-server](https://stackoverflow.com/questions/14118076/socket-io-server-to-server/14118102) Then you just remove the possibility to write from the server that listen to the other since the communication will be one way. – Fredrik Jun 15 '20 at 09:42
  • I'll take a look to this. By the way yes, one server will work "as a client" –  Jun 15 '20 at 10:14