Introducing NetMQ.WebSockets and JSMQ

NetMQ version 3.3.10.0 introduced Stream socket type, which added the ability to read raw data from a TCP socket.
Today I want to introduce what you can do with Stream socket type and what I think could be a great development for NetMQ.

Let’s start with NetMQ.WebSockets, NetMQ.WebSockets is an extension to NetMQ which adds WebSocket transport as an extension.

Because NetMQ doesn’t have a pluggable transport feature, NetMQ.WebSockets actually wraps NetMQ and provides a new socket object which has a very similar interface as the NetMQ socket.
NetMQ.WebSockets currently implements only Router and Publisher patterns.

So who can communicate with NetMQ.WebSockets? Time to introduce JSMQ.

JSMQ is a NetMQ/ZeroMQ client in javascript whose API is very similar to other zeromq bindings and can communicate with NetMQ.WebSockets.

Now some of the C/C++ or even Java gurus need to throw down the gauntlet and implement WebSocket extension for zeromq/JeroMQ and then we will have a javascript library that can talk to all zeromq implementations.

You can find the projects on github:
https://github.com/somdoron/NetMQ.WebSockets
https://github.com/somdoron/JSMQ

You can download both JSMQ and NetMQ.WebSockets from nuget (make sure to choose prerelease) or visit there pages:
https://www.nuget.org/packages/NetMQ.WebSockets
https://www.nuget.org/packages/JSMQ

And now let’s see some examples:

NetMQ.WebSockets example

static void Main(string[] args)
{
  using (NetMQContext context = NetMQContext.Create())
  {
    using (WSRouter router = context.CreateWSRouter())
    using (WSPublisher publisher = context.CreateWSPublisher())
    {
      router.Bind("ws://localhost:80");
      publisher.Bind("ws://localhost:81");
 
      router.ReceiveReady += (sender, eventArgs) =>
      {
        byte[] identity = eventArgs.WSSocket.Receive();
        string message = eventArgs.WSSocket.ReceiveString();
 
        eventArgs.WSSocket.SendMore(identity).Send("OK");
 
        publisher.SendMore("chat").Send(message);
      };
 
      Poller poller = new Poller();
      poller.AddSocket(router);
    }
  }
}

JSMQ example

Javascript File

var dealer = new JSMQ.Dealer();
dealer.connect("ws://localhost");
 
// we must wait for the dealer to be connected before we can send messages, 
// any messages we are trying to send while the dealer is not connected will be dropped
dealer.sendReady = function() {
    document.getElementById("sendButton").disabled = "";
};
 
var subscriber = new JSMQ.Subscriber();
subscriber.connect("ws://localhost:81");
subscriber.subscribe("chat");
 
subscriber.onMessage = function (message) {
   // we ignore the first frame because it's topic
   message.popString();
 
   document.getElementById("chatTextArea").value =
     document.getElementById("chatTextArea").value +
     message.popString()  + "\n";
};
 
dealer.onMessage = function (message) {
    // the response from the server
    alert(message.popString());
};
 
function send() {
   var message = new JSMQ.Message();
   message.addString(document.getElementById("messageTextBox").value);
 
   dealer.send(message);
}

HTML File

<textarea id="chatTextArea" readonly="readonly"></textarea> 
<label>Message:</label>
<input id="messageTextBox" type="text" value="" /> 
 
<button id="sendButton" onclick="javascript:send();" disabled="disabled"> Send </button>