3h-room
v0.2.0
Published
A simple lib for SSE room management.
Downloads
13
Readme
3h-room
A simple lib for SSE room management.
Example
Here's an example SSE room server based on express:
const express = require('express');
const SSE = require('3h-sse');
const HR = require('3h-room');
const app = express();
const createSSEController = () =>
new SSE.SSEController({
backend: new SSE.NodeJSBackend(),
})
const room = new HR.Room({
maxMemberCount: 2,
sseController: createSSEController(),
});
room.on('enter', (member) => {
member.sendEvent('info', 'welcome');
})
app.get('/sub/:name', (req, res) => {
const member = new HR.Member({
identity: decodeURIComponent(req.params.name),
response: res,
sseController: createSSEController(),
});
member.on('enter', (_room) => {
_room.sendEvent('debug', 'member entered: ' + member.identity);
});
member.on('leave', (_room) => {
_room.sendEvent('debug', 'member left: ' + member.identity);
});
try {
room.addMember(member);
} catch (error) {
res.status(403);
res.end();
return;
}
res.once('close', () => {
room.removeMember(member);
});
});
app.listen(8080);