Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@cryb/mesa
Advanced tools
Mesa is a WebSocket library that provides extra features such as heartbeats, automatic reconnection handling and Pub/Sub support.
ws, which Mesa wraps, on its own usually isn't enough. It doesn't provide features out of the box required by modern applications which means many users either stick to Socket.IO or write their own implementations around the ws
package. Mesa was extracted from the WebSocket implementation in @cryb/api
after we wanted to add robust WebSocket capabilities to other services.
In a nutshell, Mesa provides a simple, configurable wrapper that provides support for pub/sub, authentication, heartbeats and more and has powered production applications with millions of users since mid-2019.
This library is available on the NPM registry. To install, run:
npm install @cryb/mesa --save
If you're using Yarn, run:
yarn add @cryb/mesa
Import the library as you would with any other Node package:
const Mesa = require('@cryb/mesa').default
// or using ES modules
import Mesa from '@cryb/mesa'
To create a Mesa server, simply write:
const mesa = new Mesa({ port: 4000 })
We provide expansive configuration support for customising Mesa to your needs. See Server Configuration for options.
Mesa uses EventEmitter
in order to inform the application of events. Here's an example of a simple Mesa application:
mesa.on('connection', client => {
console.log('Client connected')
client.on('message', message => {
const { data, type } = message
console.log('Received', data, type)
})
client.on('disconnect', (code, reason) => {
console.log('Client disconnected')
})
})
Sending messages to clients or globally is easy. Simply import Message
from Mesa and use the following API:
// Sending globally
mesa.send(new Message(0, { disabled: true }, 'MANIFEST_UPDATE'))
// You can also limit to certain authenticated client ids
mesa.send(new Message(0, { content: 'Hey!' }, 'NEW_MESSAGE'), ['0', '1', '2']) // Only send to connected clients with id 0, 1, 2
mesa.send(new Message(0, { userId: '1', status: 'online' }, 'STATUS_UPDATE'), ['*'], ['1']) // Send to all connected clients except client with id 1
// Sending to clients
client.send(new Message(0, {}, 'LOGOUT'))
It's your call on how you wish to handle messages, but we recommend using a switch statement based on the type:
client.on('message', message => {
const { data, type } = message
switch(type) {
case 'STATUS_UPDATE':
handleStatusUpdate(data.status, client.id)
break
case 'TYPING_STATUS':
setUserTyping(data.typing, client.id)
break
}
})
We supply a number of guides for fully utilising Mesa server:
Note: we currently provide client libraries for Node-based JavaScript. For a browser-based client library, see mesa-js-client.
Import the Client export from the library as you would with any other Node package:
const { Client } = require('@cryb/mesa')
// or using ES modules
import { Client } from '@cryb/mesa'
To connect to a Mesa server, simply write:
const client = new Client('ws://localhost:4000')
Note: the URL provided needs to be the standard WebSocket connection URI for your Mesa server.
We provide expansive configuration support for customising the Mesa client to your needs. See Client Configuration for options.
We use the EventEmitter in order to inform the application of events from the Mesa server. Here's an example of a simple client application:
const client = new Client('ws://localhost:4000')
client.on('connection', () => {
console.log('Client connected')
})
client.on('message', message => {
const { data, type } = message
console.log('Received', data, type)
})
client.on('disconnected', (code, reason) => {
console.log('Client disconnected')
})
Sending messages to the server works the same way as sending messages to clients from Mesa:
client.send(new Message(0, { status: 'online' }, 'STATUS_UPDATE'))
Handling messages is identical to how messages are handled on the server, so again it's your choice on how you choose to implement this
We supply a number of guides for fully utilising Mesa client:
Mesa.Server
and mesa-js-client
. Source CodeMIT
If you have an issues with @cryb/mesa
, please either open a GitHub issue, contact a maintainer or join the Cryb Discord Server and ask in #tech-support
.
FAQs
A scalable, modern & robust WebSocket wrapper
We found that @cryb/mesa demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.