Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Last active January 18, 2020 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsh-code/fd4fa1d1609a861f78cddd7a5e57b385 to your computer and use it in GitHub Desktop.
Save tsh-code/fd4fa1d1609a861f78cddd7a5e57b385 to your computer and use it in GitHub Desktop.
Simple WebSocket Api
const WebSocket = require("ws");
const wss = new WebSocket.Server({ port: 8080 });
const mockedUsers = [
{
id: 1,
firstname: "John",
lastname: "Doe"
}
];
wss.on("connection", ws => {
ws.on("message", data => {
const message = JSON.parse(data);
if (message.type === "get-users") {
ws.send(JSON.stringify(mockedUsers));
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment