ws-stomp is a simple Node.js server base on websocket stomp
npm install ws-stompshell
npm i ws-stomp
`
$3
`ts
import { createServer } from 'http';
import wsstomp from 'ws-stomp';
const server = createServer();
wsstomp.server(server, '/ws');
server.listen(3000);
// ws://lcalhost:3000/ws
`
$3
`ts
import wsstomp from 'ws-stomp';
function publish() {
wsstomp.send('/topic/something', JSON.stringify({ name: 'name' }), { token: 'token' });
}
`
$3
`ts
import wsstomp from 'ws-stomp';
function subscribe() {
wsstomp.subscribe('/topic/greetings', (e) => {
const body = e.body;
});
}
`
$3
`ts
import wsstomp from 'ws-stomp';
function unsubscribe() {
wsstomp.unsubscribe('/topic/greetings');
}
`
$3
$3
`ts
import express from 'express';
import wsstomp from 'ws-stomp';
const app = express();
app.get('/send', (_, res) => {
wsstomp.send('/topic/something', 'payload');
res.status(200).json({});
});
const server = app.listen(3000);
wsstomp.server(server, '/ws');
wsstomp.subscribe('/topic/greetings', (message) => {
console.log(message.body);
});
`
$3
`ts
import { Client } from '@stomp/stompjs';
const client = new Client({
brokerURL: 'ws://lcalhost:3000/ws',
onConnect: () => {
client.publish({ destination: '/topic/greetings', body: 'Hello World!' });
client.subscribe('/topic/something', (message) => {
console.log(message.body);
});
},
});
client.activate();
``