Skip to content

MessagePack implementation over WebSockets for Node.js and JavaScript/TypeScript

License

Notifications You must be signed in to change notification settings

zo-el/msgpack-rpc-websockets

Repository files navigation


WebSockets for Node.js and JavaScript/TypeScript with MessagePack support on top.





About

This is a fork of rpc-websockets The msgpack-rpc-websockets library enables developers to easily implement their business logic that includes messaging between users, machines or any devices. It provides a possibility to send and receive JSON data through the WebSocket communication protocol in order to support two-way notification push, running RPC methods and firing any types of event signalling. Only clients can call RPC methods and not vice versa at the moment. Both frontend (HTML/JS-based) and backend (Node.js-based) development environments are supported.

msgpack-rpc-websockets is built on Node.js and supports both LTS and Current versions.

Use the free OSS edition in order to implement and manage your own WebSocket server instances.

Quick start

Install our OSS library in your project:

npm install msgpack-rpc-websockets

Write your source code using msgpack-rpc-websockets:

var WebSocket = require("msgpack-rpc-websockets").Client;
var WebSocketServer = require("msgpack-rpc-websockets").Server;

// instantiate Server and start listening for requests
var server = new WebSocketServer({
  port: 8080,
  host: "localhost",
});

// register an RPC method
server.register("sum", function (params) {
  return params[0] + params[1];
});

// ...and maybe a protected one also
server
  .register("account", function () {
    return ["confi1", "confi2"];
  })
  .protected();

// create an event
server.event("feedUpdated");

// get events
console.log(server.eventList());

// emit an event to subscribers
server.emit("feedUpdated");

// close the server
server.close();

// instantiate Client and connect to an RPC server
var ws = new WebSocket("ws://localhost:8080");

ws.on("open", function () {
  // call an RPC method with parameters
  ws.call("sum", [5, 3]).then(function (result) {
    require("assert").equal(result, 8);
  });

  // send a notification to an RPC server
  ws.notify("openedNewsModule");

  // subscribe to receive an event
  ws.subscribe("feedUpdated");

  ws.on("feedUpdated", function () {
    updateLogic();
  });

  // unsubscribe from an event
  ws.unsubscribe("feedUpdated");

  // login your client to be able to use protected methods
  ws.login({ username: "confi1", password: "foobar" })
    .then(function () {
      ws.call("account").then(function (result) {
        require("assert").equal(result, ["confi1", "confi2"]);
      });
    })
    .catch(function (error) {
      console.log("auth failed");
    });

  // close a websocket connection
  ws.close();
});

Documentation

Please consult our API documentation for both WebSocket server and client JavaScript and TypeScript classes.

OSS Features

Features of the free open-source edition.

OSS Features

All library's open-source features are documented in our API documentation and can be used free of charge. You are free to implement your solutions based on provided methods in any way you are comfortable with, as long as you use our work along our very permissive license conditions.

License

This library is licensed under LGPLv3. Please see LICENSE for licensing details.

About

MessagePack implementation over WebSockets for Node.js and JavaScript/TypeScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published