Show HN: Socket-call – Call socket.io events like normal JavaScript functions
By
bperel
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(
'/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 URL: https://news.ycombinator.com/item?id=44288643
Points: 5
# Comments: 0
You might also wanna read
Malaysia enforces social media ban for children under 16, requiring age verification from platforms
Malaysia has begun enforcing a ban on social media accounts for children under 16, requiring platforms like Facebook, Instagram, TikTok, and
NVIDIA RTX Spark — Slim Laptops & Small Desktops
Microsoft unveils Surface Laptop Ultra with NVIDIA RTX Spark for creators and developers
Microsoft announces the Surface Laptop Ultra, a high-end laptop co-engineered with NVIDIA and optimized for RTX Spark. The device is positio
Vera C. Rubin Observatory Set to Discover Millions of Asteroids and Transient Phenomena in Big-Data Astronomy Era
The Vera C. Rubin Observatory in Chile is preparing to begin operations, designed to capture the entire Southern Hemisphere night sky every
Stoicism and Overthinking: How Marcus Aurelius' Private Writings Offer Ancient Tools for Modern Anxiety
This article from "The Examined Life" newsletter (Issue #4) explores Stoic philosophy through the lens of Marcus Aurelius, who wrote his fam
From Bottleneck to Empowerer: Applying the Leader-Leader Model in Engineering Management
The article discusses the common challenge engineers face when transitioning from individual contributor roles to engineering leadership pos
