fruster-web-bus-client
v0.0.8
Published
client for using the fruster web bus
Downloads
3
Readme
Fruster web bus client
The fruster bus client looks at the subject of incoming messages from the server and redirects them to the corresponding handlers. All handlers for a specific subject will be triggered by an incoming message. By default, the subscribe function registers a handler on the specific subject with an id of that subject to prevent stock up in a reactive application. If a specific subject needs several things to happen in different places in the application, unique ids can be given to make sure they don´t overwrite each other.
Subjects are defined by the backend services in similar fashion to ws.out.:userId.*
.
Example subject: ws.out.d5d20597.video-session.start
Instantiate the bus:
var bus = new FrusterWebBusClient("ws://localhost:3000");
Register a handler for a specific subject. Every new subscribe will overwrite any previous handler (without id) for that subject.
bus.subscribe("ws.out.d5d20597.new-message", (msg) => {
console.log("Subscribed to subject ws.out.d5d20597.new-message");
});
Register a handler for a specific subject without overwriting previous handlers using ids (id = VIDEO_SESSION_HANDLER_ID):
bus.subscribe("ws.out.d5d20597.video-session.start", "VIDEO_SESSION_HANDLER_ID", (msg) => {
console.log("Subscribed to subject ws.out.d5d20597.video-session.start with id VIDEO_SESSION_HANDLER_ID");
});
Register a handler for a specific subject overwriting previous handler using a specific id (id = VIDEO_SESSION_HANDLER_ID):
bus.subscribe("ws.out.d5d20597.video-session.start", "VIDEO_SESSION_HANDLER_ID", (msg) => {
console.log("overrides the previous handler for subject ws.out.d5d20597.video-session.start with id VIDEO_SESSION_HANDLER_ID");
});
Remove a handler for a specific subject and id (id = VIDEO_SESSION_HANDLER_ID):
bus.unsubscribe("ws.out.d5d20597.video-session.start", "VIDEO_SESSION_HANDLER_ID");
Remove all handlers for a specific subject:
bus.unsubscribe("ws.out.d5d20597.video-session.start");
Register bus error handler:
bus.onError(() => {
console.error("ERROR");
});
Register bus open handler:
bus.onOpen(() => {
console.error("OPENED");
});
Register bus close handler:
bus.onClose(() => {
console.error("CLOSED");
});
Requests
Requests can be made to specific fruster websocket endpoints. The endpoints uses the same methods as http, e.g. POST, GET etc. Example endpoint ws.get.user.:userId
which can be called as ws.get.user.4b2a50c5-f8fc-465c-a8e1-bfa978767396
to get the user with the id 4b2a50c5-f8fc-465c-a8e1-bfa978767396
.
const response = await bus.request({
subject: `ws.post.chat`, // endpoint
message: { // request body
data: {
message: $("#message").val()
}
}
});
Response is returned in the same fashion as http endpoints in fruster.
For more examples, see FrostDigital/fruster-web-bus-client examples
Development
To start webpack in watch mode:
npm start
To build project using webpack and generate typings:
npm run build