ws-stomp-server
v1.0.8
Published
ws-stomp-server
Readme
ws-stomp-server
ws-stomp-server is a simple ws stomp server
install
npm i ws-stomp-serverhttp
import { createServer } from 'http';
import { Stomp } from 'ws-stomp-server';
const server = createServer();
Stomp.server(server, '/ws');
// ws://lcalhost/wsexpress
import express from 'express';
import { Stomp } from 'ws-stomp-server';
const app = express();
const server = app.listen();
Stomp.server(server, '/ws');
// ws://lcalhost/wssend
import { Stomp } from 'ws-stomp-server';
function publish() {
Stomp.send('/example', JSON.stringify({ name: 'example' }), { token: 'example' });
}subscribe
import { Stomp } from 'ws-stomp-server';
function subscribe() {
Stomp.subscribe('/example', (e) => {
const body = e.body;
});
}unsubscribe
import { Stomp } from 'ws-stomp-server';
function unsubscribe() {
Stomp.unsubscribe('/example');
}server and client
server.js
import express from 'express';
import { Stomp } from 'ws-stomp-server';
const app = express();
app.get('/send', (_, res) => {
Stomp.send('/topic/something', 'payload');
res.status(200).json({});
});
const server = app.listen(8080);
Stomp.server(server, '/ws');
Stomp.subscribe('/topic/greetings', (message) => {
console.log(message.body);
});browser.js
import { Client } from '@stomp/stompjs';
const client = new Client({
brokerURL: 'ws://localhost:8080/ws',
onConnect: () => {
client.publish({ destination: '/topic/greetings', body: 'Hello World!' });
client.subscribe('/topic/something', (message) => {
console.log(message.body);
});
},
});
client.activate();