gj / fastify-ws Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
https://github.com/ClusterWS/cWS
I've used uws-> ws-> cWS.
ws really cause a lot of memory consumption.
cWS is based the author of uws's new project, but maintain mostly the same API (not 100%).
So I suggested to use this project to replace uws for long term and gain low memory footprint.
How can I broadcast one message from a route? Is it possible?
Can you please add example?
Please disable fastify.server when noServer:true.
Hey,
I've tried to listen for when a websocket disconnects using:
ws.on('connection', (socket) => {
console.info("Someone connected!");
socket.on('message', (data) => {
});
socket.on('disconnect', () => {
console.info("someone disconnected");
});
});
disconnect never gets called. Is there a way to know when someone disconnects?
utf-8-validate it's outdated and throws a 404 during the build
prebuild-install http 404 https://github.com/websockets/utf-8-validate/releases/download/v3.0.4/utf-8-validate-v3.0.4-node-v64-win32-x64.tar.gz
That causes to fail the entire installation.
EDIT: The issue is only present on node v10, works fine on the LTS release
Please see fastify/fastify#499
fastify.ready(err => {
if (err) throw err
fastify.ws
.on('connection', socket => {
socket.on('message', msg => socket.send(msg)) // Creates an echo server
})
})
Errors
[ts] Argument of type '(err: any) => void' is not assignable to parameter of type '() => void'.
(parameter) err: any
[ts] Property 'ws' does not exist on type 'FastifyInstance<{}, {}, {}>'.
Had to tweak it to the following to make it compile
fastify.ready(() => {
fastify['ws']
.on('connection', socket => {
socket.on('message', msg => socket.send(msg)) // Creates an echo server
})
})
Hi,
I'm trying to implement your plugin in Fastify 2.0 but it's doesn't work.
fastify.ready(err => {
if (err) throw err
console.log('Server started.')
fastify.ws
.on('connection', socket => {
console.log('Client connected.')
socket.on('message', msg => socket.send(msg)) // Creates an echo server
socket.on('close', () => console.log('Client disconnected.'))
})
})
I don't see any error but my client can't connect to my websocket server.
Can you help me ?
thank you
Hi! ๐
Is the ping interval handling mandatory?
Use setInterval
is not a very good idea, does not scale well and maybe the users do not need it.
Maybe expose a setKeepAlive
function and let the user decide how handle this?
I get the following error message, just before disconnecting / Refreshing page / Socket Closing:
{"type":"Error","message":"read ECONNRESET","stack":"Error: read ECONNRESET\n at TCP.onread (net.js:622:25)","errno":"ECONNRESET","code":"ECONNRESET","syscall":"read"}
My code below:
Note that I first get the error message above and then my 'Client Disconnected' log message.
app.ready(err => {
if (err) throw err
app.log.info('Server started')
app.ws.getUniqueID = function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
}
return s4() + s4() + '-' + s4()
}
app.ws
.on('connection', socket => {
socket.id = app.ws.getUniqueID()
app.log.info('Client Connected: ' + socket.id)
socket.on('message', msg => {
socket.send(JSON.stringify({
from: socket.id,
to: socket.id,
msg: msg
}))
})
socket.on('close', () => {
app.log.info('Client Disconnected: ' + socket.id)
})
})
})
I want to update to fastify 2.0 but this is the only plugin i use that is not ready for it. I tried to do a PR but I have no rights to do commits.
Hi,
Currently, I'm running with a single socket. How I can open multiple socket connects with different routes. My single-socket example.
require('dotenv').config();
const fastify = require('fastify')({ logger: true });
fastify.register(require('fastify-cors'), {
origin: '*',
});
fastify.register(require('fastify-ws')); // Registering websocket
function connectToSocket(ws, req) {
console.log('Clinet got connected');
let reqId = req.url;
ws.on('message', (data) => {
// handle messages that send from client to server
data = JSON.stringify(data);
console.log(data)
ws.send(data);
});
}
/**
* starting the server
*/
const start = async () => {
try {
await fastify.listen(process.env.APP_PORT || 8884, process.env.APP_HOST || '0.0.0.0');
fastify.ws.on('connection', connectToSocket); // Initializing the socket controller
} catch (error) {
fastify.log.error(error);
process.exit(1);
}
};
start();
module.exports = fastify;
with this how I can open multiple socket connections. Please help.
Hi,
I used forked repo from https://github.com/oransel/fastify-simple-ws and while plugin loads I can't test for any sent messages using https://amritb.github.io/socketio-client-tool/. I have a public POC repo https://github.com/johntom/Fastify-Demo2 and any help with sockets much appreciated.
resgistration in plugins/websocket.js everything at first seems fine but I had to change
fastify.register(require('fastify-ws'))
to
fastify.register(require('fastify-simple-ws'))
I try to publish message in services/hello.js but socket.on does not fire
I've only used sockets in a sails,js app and a test with feathers.js so I don't have tons of experiance in this area as both frameworks made it very easy
Thanks,
John
When I Install it, the following warning message appears:
npm WARN deprecated [email protected]: New code is available at github.com/uNetworking/uWebSockets.js
Would you mind if I prepare a PR for this?
Fastify 1.7.0-2 should be a peer dependency so that users get a static warning about their fastify version
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.