
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
wspromisify
Advanced tools
Makes websocket's API just like REST with Promise-like API, with native Promises. Has yummies and very lightweight!
// If you detected some bug or so, please, fill an issue. // Large data support, streams and different server-side implementations are coming.
Makes a Promise-like WebSocket connection. Features (almost all are tunable via constructor config below.)
How it on Server Side ?
1. Serialized JSON is sent by this lib = {id: 'generated_id', data: your data}
2. Some Server processing...
3. Serialized JSON is sent back by the Server = {id: 'the same generated_id', data: feedback data}
Default constructor config is
{
// You can also use plain text and blobs in future.
data_type: 'json',
// Debug features. Not required.
log: ((event, time, message) => null),
// Will count milliseconds for responses and put them to log function above.
timer: false,
// Set up.
// Required. URL to connect.
url: 'localhost',
// Timeout after sending a message before it dropes with error.
timeout: 1400,
// Reconnect timeout in seconds or null.
reconnect: 2,
// Lazy connect: connects only if something sent (then sends all of them!)
lazy: false,
// You can set your own middleware here.
adapter: ((host, protocols) => new WebSocket(host, protocols)),
// WebSocket constructor's protocol field.
protocols: [],
// Unique id's and data keys to negotiate with back-end.
server: {
id_key: 'id',
data_key: 'data'
}
}
Fields/Props:
// read-only, returns WebSocket (or so) instance to use with other stuff.
socket
Methods:
// sends any type of message.
send(message),
// .addEventListener with optional predicate.
on(event_name, handler, predicate = (WebSocketEvent) => true),
// Closes the connection and free up memory.
close()
Example:
import WSP from 'wspromisify'
const somehost = 'example.com:8080'
const someFunction = async () => {
const ws = new WSP({
url: `${somehost}/ws`,
timeout: 2e3,
timer: true,
log(event, time, message = '') {
if(time !== null) {
console.log(event, `in ${time}ms`, message)
} else {
console.log(event, message)
}
}
})
try {
const data = await ws.send({catSaid: 'Meow!'})
console.log({data})
} catch(error) {
console.error('Cannot send a message due to ', error)
}
}
someFunction()
FAQs
Wraps your WebSockets into Promise-based class with full d.ts typings on client & server
The npm package wspromisify receives a total of 68 weekly downloads. As such, wspromisify popularity was classified as not popular.
We found that wspromisify demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.