Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created August 25, 2019 22:33
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 mizchi/071769217d5f765ec683637be2a7b8f8 to your computer and use it in GitHub Desktop.
Save mizchi/071769217d5f765ec683637be2a7b8f8 to your computer and use it in GitHub Desktop.

slink

Comlink like RPC for socket.io

// server
const handlerMap = {
  async foo(args: { id: string }, socket) {
    return "foo:" + args.id + ":" + socket.id;
  }
}
io.on("connection", socket => {
  buildSocket(socket, handlerMap);
});

// client
import { wrapSocket } from "slink";

const socket = io();
socket.on("connect", async () => {
  const wrapped = wrapSocket(socket);
  const res = await wrapped.call("foo", { id: 'a' });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment