Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Pomax opened this issue Jun 1, 2023 · 0 comments
Open

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

Pomax opened this issue Jun 1, 2023 · 0 comments

Comments

@Pomax
Copy link

Pomax commented Jun 1, 2023

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 =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant