webstomp
Read about STOMP protocol here.
It seems STOMP is under-appreciated for realtime apps. It's text-based so it can work over WebSocket connections. My hope is to provide enough structure to make STOMP servers as simple as express HTTP servers.
The motivation for using STOMP instead of socket.io
or SocketCluster
or Sails
or ActionCable
is that STOMP is an open protocol which can run on any client or server the supports TCP connections. Things change, perhaps in the future you'll want to use golang
on the server and native iOS
sockets.
Each package module is explained below, from lowest to highest abstraction.
Frame
Each message between client and server is a "frame", which looks like:
var frame = command: "string" headers: "string": "string" body: "string" var text = Frameframe = Frame
The STOMP protocol defines available commands and headers. webstomp
checks if the command is valid.
Socket
The webstomp
Socket encodes and decodes frames. Otherwise, you'd have to call toString
and fromString
on each send and receive.
var WebSocket = var Socket = var ws = "https://example.com"var socket = ws socket socket
Server
The webstomp
Server emits Socket connections. Otherwise, you'd have to wrap each new WebSocket connection yourself.
var Server = var server = port: 8000 server
You're bored. Here's the useful bits coming up.
Session
The webstomp
Session adds a number of helpers. Otherwise you'd have to send full frames and stringify javascript objects.
var Session = // `socket` is a webstomp Socketvar session = socket // Send connected framesession // Send json message to a channelvar data = key: "value" var headers = "channel": "/notifications" session // Send errors to clientsession
For realtime apps, it's practical to model channels as lazy observables. Sessions can pipe observable events to the client as message frames. Examples include es-observable
, kefir
or baconjs
stream, readable event-stream
, etc.
// When client subscribesvar unhook = session // When client unsubscribes
Router
The webstomp
Router is designed to look like express. Instead of HTTP actions, you have STOMP actions. Instead of (req, res, next)
you have this (next)
.
var Router = var router = router router router router router
App
The webstomp
App ties it all together. It creates a WebSocket server, wraps it with a webstomp
Server, acts like a webstomp
Router, and dispatches request Frames to the current Session.
var App = var app = // Or you can be fancy// var app = require("stomp")() app app app
Mount Server
You can mount a webstomp
App onto an HTTP server, which allows you to use the HTTP server too. For example, with express:
var http = var stomp = var api = var server = stomp server
So that's it. For realtime apps, on-demand PUB/SUB channels provide structure. The pattern maps well to server-side services that return an observable, and also client-side components that subscribe when added into view, and unsubscribe when removed.
Open Source
Please contribute ideas, bugs, etc.
MIT License