frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Show HN: Socket-call – Call socket.io events like normal JavaScript functions

https://github.com/bperel/socket-call
24•bperel•4h ago
Hello HN,

I built a Typescript library (named socket-call, for lack of a more sexy name) whose goal is to be able to call socket.io events as regular functions.

So you declare your server-side like so:

  ...
  const listenEvents = (services: UserServices) => ({
    // Add your events here, the name of the event is the name of the function
    login: async (username: string) => {
      services._socket.data.user = { username };
      console.log(`User ${username} logged in`);
      setInterval(() => {
        // Calling an event that's handled client-side
        services.showServerMessage(`You're still logged in ${username}!`)
      }, 1000);
      return `You are now logged in ${username}!`;
    },
  });
and then on the client side you call them like normal async Javascript functions (and you can also create client-side event handlers):

  ...
  const user = socket.addNamespace<UserEmitEvents, UserListenEvents>(
    '/user'
  );
  
  // Calling an event that's declared server-side
  user.login(username.value).then((message) => {
    console.log('Server acked with', message);
  });
  
  // Handling an event that is sent by the server
  user.showServerMessage = (message) => {
    console.log('Server sent us the message', message);
  }

I use this library for my own projects and would be interested to receive feedback about it :-)

Comments

klabb3•3h ago
This appears to me like the NATS ”request-response” pattern. They also have first-class support for this in their client libs. Under the hood, they create and subscribe to an ephemeral topic where servers can send the response to. (Perhaps even streamed multiple responses but you’d need to double check that.) They also have websocket support btw, so it can be used by web browsers.
chrisweekly•3h ago
I like the ergonomics, this looks like it could be useful. Thanks for sharing!
benpacker•1h ago
You can do with with trpc WebSocket transport
xixixao•1h ago
Convex[0] also gives you type-safe persistence (in addition to type-safe web-socket communication).

[0] https://docs.convex.dev/quickstart/script-tag

ossobuco•1h ago
I don't know, socket.io already feels like an unnecessary abstraction to me, and this is another abstraction on top of it. I generally dislike APIs that hide what's happening under "magic" abstractions, plus this seems leaky, as it abstracts on socket.io but requires you to know how it works.
imtringued•4m ago
socket.io is probably one of the most unnecessary libraries on this planet. Websockets are already as simple as possible.

In fact, websockets work so well I use them as a generic TCP replacement, because the message oriented transport model gives me 99% of what I need with the exception of custom message types. Leaving that out was a massive letdown to me, because you now need to carry a way to identify the message type inside the body, rather than just throwing the message itself into the appropriate protocol parser (e.g. a schema based binary format).