HenningM/express-ws

Adding a `socket.json()` to mirror Express' `res.json()`

Opened this issue · 0 comments

Pomax commented

Having a socket.send() that mirrors the standard express res.send() is nice, but having an extra quality-of-life socket.json() that mirrors res.json(), where it automatically JSON-serializes whatever you pass as argument and sends that over the wire would be quite nice. Of course, folks can trivially implement this themselves, e.g.

socket.json = (payload) => socket.send(JSON.stringify(payload));

but they shouldn't have to. They're using express, a web socket version of res.json(), since we're using web sockets and we're guaranteed to be sending JSON back and forth a lot, would make a whole lot of sense.

With bonus points for socket.on("json", ... ) that passes the event handler the JSON.parse'd data. It (barely, but just about) makes sense that the plain ws package does not come with this, but when you're bolting web sockets into express, JSON is kinda of a given =)