Reactive Binary Messaging Protocol
npm install rbmpts
const prot = location.protocol.includes( 'https' ) ? 'wss' : 'ws';
const host = window.location.hostname;
const conn = new WebSocketConnector( () =>
new WebSocket( ${ prot }://${ host } )
);
`
Creating connection in NodeJS app
Sample code to create connection in nodejs app:
`ts
import * as WebSocket from 'ws';
...
const addr = wss://127.0.0.1;
const conn = new WebSocketConnector( () =>
new WebSocket( addr, { rejectUnauthorized: false } )
);
`
Encoding complex object
Sample code to encode data using JSON type definition:
`ts
const msg = new ByteArrayMessage( 'Custom Topic' );
msg.write( complexObj, {
uint32Prop: 'uint32',
strProp: 'str',
optionalInt64Prop: [ '?', 'int64' ],
arr: [ 'array', 'int16' ],
obj: {
bufProp: 'buf',
saveTime: 'datetime'
},
map: [ 'map', 'uint64', 'str' ]
} );
`
Streaming binary data
Sample code to stream binary data:
`ts
// stream buffers
conn.on( 'message', data => {
console.log( Buffer received: ${ data.byteLength } );
} );
...
// stream messages
conn.emit( msg => {
console.log( Message received: ${ msg.topic } );
} );
`
Implementing request/response
Sample code to request some server data:
`ts
// client app: send a request message and await to receive response message
const conn = new WebSocketConnector( () => new WebSocket( 'ws://localhost:8000' ) );
await conn.once( 'connected' );
const req = new ByteArrayMessage( 'Topic 101010' );
req.writeInt32( 101011 );
req.writeString( 'text' );
req.writeNullable( true, req.writeBoolean );
req.writeArray( [ 123, 456 ], req.writeUint32 );
const res = await conn.post( req );
console.log( Received response ${ res.topic } );
...
// server app: create WebSocket server and controller to reply
const controller = new WebSocketController();
const wss = new WebSocket.Server( { port: port } );
controller.emit( ( ws, msg ) => {
console.log( 'received request' );
const val = msg.readInt32();
const res = new ByteArrayMessage( msg.topic );
res.writeInt32( -val );
controller.send( ws, res );
} );
wss.on( 'connection', ( ws, req ) => {
controller.add( ws, req.socket.remoteAddress );
} );
`
Implementing server managed publish/subscribe
Sample code to subscribe to some server message streams:
`ts
// client app: subscribe to the topic 'SomeTopic'
conn.subscribe( 'SomeTopic', msg => {
console.log( Message ${ msg.topic } );
} );
...
// client app: subscribe to messages on topic 'MyTopic' for 5 seconds
const abortController = new AbortController();
conn.subscribe( 'MyTopic', msg => {
console.log( Subscription message ${ msg.topic } );
}, abortController.signal );
setTimeout( () => {
abortController.abort();
}, 5000 );
...
// server app: create WebSocket server and controller to reply
const controller = new WebSocketController();
controller.emit( ( ws, msg ) => {
console.log( 'received subscription' );
controller.subscribe( ws, msg );
} );
const wss = new WebSocket.Server( { port: port } );
wss.on( 'connection', ( ws, req ) => {
controller.add( ws, req.socket.remoteAddress );
} );
setTimeout( () => {
const publication = new ByteArrayMessage( 'MyTopic' );
publication.writeBigInt( 123n );
publication.writeString( 'Test' );
wsc.publish( publication );
}, 10000 );
``