Max Methot
Max Methot

Reputation: 128

Ideas how to implement a client-server-client infrastructure in javascript/HTML5/NodeJS

I'm currently working on a project where the infrastructure must look like this:

[Client 1] ----------- [Server] ----------- [Client 2]

Client 1 must activate a button, which would trigger an event on the server. Then, client 2 must listen for that specific event to show the value.

For instance, Client 1 would be a tablet, with a simple button that would have an ID of 1, let's say.

I have to be able to show on Client 2, let's assume a TV plugged on a linux (Raspberry Pi) machine, that button ID 1 was pressed on the tablet.

How would you implement that architecture, using only HTML for the display of Client 1 and 2, javascript for communicating between server and clients and NodeJS as a server core?

Any idea will be welcome, I tried to implement it but my logic failed me at some point! :)

Upvotes: 3

Views: 808

Answers (1)

Slavo
Slavo

Reputation: 15463

It seems that what you describe is real-time functionality. What you are describing is essentially similar to a chat system, only a little more fine-grained and structured. It is not a matter of what technology you use to present it, but rather the protocol you use to communicate between the clients and the server.

Normally HTTP is not very well suited for real-time applications. A technology which tries to solve the problems you have is WebSockets. NodeJS supports WebSockets and new browsers also do, so you can look around for different libraries and implementations. Just have in mind that when using the WebSockets protocol, the tools that you use on the client depend on the ones you use on the server and vice versa.

You can take a look at Socket.io as an example. For further pointers, you can take a look at this question: Which websocket library to use with Node.js?

Many libraries provide one additional layer of abstraction over WebSockets and let you implement real-time functionality without caring about the underlying protocol. The Meteor framework mentioned in one of the comments is such a thing - it uses WebSockets and HTTP under the hood, depending on what you do.

It is all a matter of how much control you want. You have to know how much work you want to do yourself and how much you want done for you by a third party.

Upvotes: 3

Related Questions