Simple multi-client, multi-scope websocket server and client
npm install @jkob/another-ws-serverwebsocket package.
js
const Server = require('@jkob/another-ws-server/server');
const server = new Server({
allowedOrigins: [], // allow all if empty or missing
protocol: 'echo-protocol',
port: 8080
});
await server.start();
...
server.stop();
`
How to use client
`js
const Client = require('@jkob/another-ws-server/client');
const client = new Client({
host: 'ws://...',
protocol: 'echo-protocol',
pid: 'project-id'
});
await client.connect();
client.send('chat:message', 'Your Message');
client.on('chat:message', (message) => {
console.log('Message', message);
});
client.close();
`
How to use web client
`js
const WebClient = require('@jkob/another-ws-server/webClient');
const webClient = new WebClient({
host: 'ws://...',
protocol: 'echo-protocol',
pid: 'project-id'
});
await webClient.connect();
webClient.send('chat:message', 'Your message from browser');
webClient.on('chat:message', (message) => {
console.log('Message', message);
});
webClient.close();
``