How to build mqtt broker from scratch? - protocols

I need to build a MQTT broker with basic functions but I cannot find any documents about MQTT broker.
Anyone have any idea how to do this? What do I need to read?
Firstly, I just want broker can accept connection using CONNECT and CONNACK.

The MQTT specification is available here, this will outline the protocol you will need to implement.
If your question is more generically, "How do I implement a network protocol?" then I would have to ask why you think you need to write your own broker and not just use one of the existing ones available. Even if the existing open source brokers don't do exactly what you want, adapting one of these will be much easier than starting from scratch. Brokers like Mosca and Moquetta allow themselves to be embedded into other applications.
If you still feel you need to write your own then I would start by picking one of the existing open source brokers and see how they have gone about it, picking one in a language similar to the one you intend to use would be the best bet.

Related

What’s the best way to architect a MQTT broker and display data on a web page?

I’m not pretty sure if It's appropriate to post this question here, but I couldn’t find anyone or any article that could clarify that to me.
Anyway, the doubt is the following: I'm currently working on a project which intends to monitor a bunch of data that is streaming over the CAN protocol, this data is sent by some devices. Well, I came up with the idea to monitor and display this data using the MQTT protocol along with NodeJS.
I managed to develop a broker using mosquito running in a docker container, I can receive the messages and print them on my terminal, In addition, I found out on the internet that I could use Express Ws to create a WebSocket, so I could send the data to my Web page, so far it seems to be working as expected. The problem is I don’t know if that's the right way to do that. I've got this concept in mind but seems to be too simple.
The questions are:
How should I manage many publishers taking into consideration just one broker?
Should I have a MQTT broker for the server and another one for client?
Is it right to consume the data through a websocket?
I apologize in advance for this whole question, I just want some guidance.

Apache Cassandra integration with RabbitMQ

I want to use MQTT as a protocol communication with RabbitMQ Message Broker, but from rabbitmq website I found this paragraph:
These implementations are suitable for development but sometimes won't be for production needs. MQTT 3.1 specification does not define consistency or replication requirements for retained message stores, therefore RabbitMQ allows for custom ones to meet the consistency and availability needs of a particular environment. For example, stores based on Riak and Cassandra would be suitable for most production environments as those data stores provide tunable consistency.
https://www.rabbitmq.com/mqtt.html
So, from this paragraph, I should to use Cassandra as a database for RabbitMQ, but I didn't find anything about integration Cassandra as a database for rabbitmq.
can you help me by giving me something to make it possible.
NB:I'm newbie in RabbitMQ.
The paragraph refers specifically to "retained messages" part of MQTT spec, as in, the messages you want to keep for a long time. Like a "last know configuration", that you may want to apply to any MQTT subscriber, regardless whether or not it has been online and subscribed at the moment the message is published.
It's a very particular situation and unless you need that feature you don't have to worry about using RabbitMQ as MQTT broker. For regular messages built-in RabbitMQ replication options are perfectly suitable and production-ready.
Until now, RabbitMQ doesn't support this feature.
so, it's not possible to use another database instead of Mnseia database

Strategy to implement a scalable chat server

I am looking to implement some sort of chat server. And I want it to scale. This seems like a big question, so I guess I expect the answers to be direction pointers, sort of exploratory.
The end-user clients are web or phone client. I think some sort of websocket implementation, such as Socket.IO is nice.
On the server side I wish to use Node.js. I want the architecture to be scalable so that the number of users are not limited (well, within reason, the chance of big hit is not expected, and if it is, the chance of having smarter, experienced people to work on it is reasonable instead of currently just me coding) The number of users per chatroom is hopefully not limited, or maybe some fixed large number. And that means I need to scale horizontally using several servers written in Node.
Suppose some load balancer (and hopefully in the future not a single point of failure, but I don't know how I would achieve that, or maybe just move to AWS) are dispatching SocketIO connections from the end clients to the chat servers. Different users connection to different servers may be in the same room, so the messages need to be send to other servers.
How would I feasibly implement something like this? Hopefully not too complex.
Questions:
(1) If all servers need to handle all messages as users can be logged on via any of the servers, does this scale?
(2) Do I need some sort of message queue for the servers to talk among them? Is Pub-sub from Rabbitmq usable for this? Or if zeromq, how would I scale with pub sub? The Zeromq guide is has explanations for scaling to more than one server with REQ/REP type of applications. But not Pub Sub.
(3) Or should I start with XMPP?
I am hoping to make it work as easy as possible.
There's a rather good explanation at the Socket.io site. Have a look at
http://socket.io/docs/using-multiple-nodes/
It suggests using Nginx as HTTP load balancer, Node.js clustering (with sticky sessions) and Redis as the message backend.
I think your goals should be achievable with little to none coding involved, only using the given modules and configuration mechanisms.

Managing RabbitMQ

I'm new to RabbitMQ and MQ's in general. I'm using the rabbit.js Node.js module to interface with RabbitMQ, so all my application layer is going to mainly be in Node.js. What I'm wondering is, how do I manage RabbitMQ? How can I see everything that's going on with RabbitMQ, from what's messages are left in the queue to general configuration and administration?
I'm looking for something visual, but more importantly, easy to use and simple.
RabbitMQ has a web interface (part of the rabbitmq_management plugin which ships with RabbitMQ, but needs to be enabled) that allows you to see the servers, exchanges, queues, etc.
It's pretty easy to use. One thing I would recommend is to set the time-interval on the graphs to 10 minutes. I find if you set them to longer, say, an hour plus, the information (due to the way it's bucketed, I think) gets a bit wonky.
Check out this link for more info: https://www.rabbitmq.com/management.html
There is also a JSON API that can be used to programmatically determine, for example, how many items are in a particular queue.
There's also a cmdline tool, called rabbitmqadmin (https://www.rabbitmq.com/management-cli.html) which can come in really handy for things like setting up test RabbitMQ test environments via a bash script and things of that nature.
check JXM.io sources (open source messaging backend for node.js / JXcore) that uses RabbitMQ for multi server integration and there is a nice article showing how to cluster RabbitMQ http://jxm.io/multi-server-messaging-backend-installation/

What's the best way to make one node.js server "talk" to another?

MsgPack ?
JSON-RPC ?
Socket.io (is it possible ? how ?)
EDIT:
I am talking about 2 node processes each one on a different physical machine;
I don't understand how redis can help me on this...
I'm not really clear on whether you are looking for ways to make two node servers on two physical machines "talk to each other", or two node.js server processes on one machine.
(You could edit your question to make it clearer).
You could look at:
protocol buffers for node
MsgPack-RPC for node
Websocket.MQ
dnode --This uses socket.io as the transport layer
IPCNode
AMQP with node-amqp or node-amqp and something like RabbitMQ
Or you could go with a document based db like redis
Note: some of these may need some updating
I hope this helps
I would go for redis. The pubsub semantics are pretty sweet. The node_redis client library is very fast because it can use the lightning fast c-extension-library named hiredis. I would just use json as my encoding. That will probably be more than fast enough.
You could also use DNode to do your communication if you like. I also believe it has socket.io capabilities. You should have a look at the source code to find this out.
It is not really clear from your question what do you mean by a Node server talking to another server. You can use anything from sending UDP packets, making TCP connections, HTTP connections to using any of the high-level mechanisms that others have already pointed out.
For an interesting scenerio of Node processes communication you may take a look at
the 2010 JSConf.eu talk by Mikeal Rogers. He explains how to use CouchDB to do that. Very interesting talk.

Resources