serverless websockets
made easy
Tap into the power of serverless technology. Say goodbye to server maintenance and hello to seamless, scalable websockets using our open source library websocks.
end-to-end typesafety
Our open-source library, websocks, is supercharged with end-to-end typesafety using zod. Never worry about sending the wrong data to the wrong place again and searching for bugs for hours.
server.ts
import { socks } from "socks";import { z } from "zod";const s = socks.init();const sender = s.sender.messages({greet: s.sender.message.payload(z.object({ msg: z.string(), newNum: z.number() })),});const receiver = s.receiver.messages({name: s.receiver.message.payload(z.object({ name: z.string(), num: z.number() })).on((ws, input) => {const newNum = input.num + 1;sender.greet({ msg: `Hi, ${input.namme}!`, newNum}) // whoops, typo.to(ws);}),});export type SocksType = typeof socks.create({sender,receiver,});
msg: string
newNum: number
client.ts
import { socksClient } from "socks/client";import type { SocksType } from "../backend/server";const client = socksClient.create<SocksType>({domain: "wss://big-red-cat.socks.so",})client.send.name({ name: "John" })client.on.greet((input) => {console.log(input.|) // autocomplete works like a charm})
websockets on steroids
Features, features and more features.
Middleware
Unlock the full potential of middleware functions, just as you're used to in Express.js.
Middleware functions provide a robust way to integrate features such as authentication, logging, and rate limiting into your application. What sets us apart? Our middleware functions are not only powerful – they're fully type-safe.
Implementing authentication for websockets has never been easier.
middleware.ts
import { socks } from "socks";import { z } from "zod";import { validate } from "./auth";// create a socks instance with types for context and headersconst s = socks.init({headers: z.object({ token: z.string() }),connect: (header) => ({user: null}),});// write your own complete typesafe middlewareconst authReceiver = s.receiver.use((ws, headers, context) => {try {const user = validate(headers.token);return { user }} catch {throw new SocksError({ code: "UNAUTHORIZED", message: "Invalid token" });}});// use middleware context in your messagesconst receiver = s.receiver.messages({name: authReceiver.message.on((ws, context) => {// context has type { user: string }sender.greet({ msg: `Hi, ${context.user}!` }).to(ws.id);}),});
Error Handling
Managing errors is a breeze with our WebSocket API. All you need to do
is throw a SocksError
and we'll take care of the rest. This includes automatically sending
an error message to the client.
On the client side, you can effortlessly implement an error handler to effectively manage and respond to any issues that may arise. Our seamless error handling ensures a smooth and trouble-free WebSocket experience. In debug mode, the error message will always be sent to the client for seamless debugging.
Payload Validation
Thanks to our built-in payload validation with zod, you can rest assured that your application will be free of any invalid data. Our WebSocket API automatically validates the payload of every incoming message, ensuring that it conforms to the specified schema. If the payload is invalid, the message will be rejected and an error messages will be sent to the client.
client.ts
/* this of course will never happen if you use thetypesafe client */client.error.invalidPayload.on((message) => {console.log("Invalid payload: " + message);});client.error.unauthorized.on((message) => {console.log("Unauthorized: " + message);});
server.ts
const receiver = s.receiver.messages({joinParty: s.message.payload(z.object({ name: z.string() })).on((ws, input) => {if (input.name === "PartyCrasher") {throw new SocketError({code: "UNAUTHORISED",message: "Sorry, you are not invited to this party! 😢",});} else {joinParty(ws);}}),});
Rooms API
Allow users to seamlessly connect to a specific rooms and exclusively exchange messages within that designated space. Think of it as a topic-oriented publish/subscribe system, particularly useful for applications like chat rooms.
With this approach, you can effortlessly manage multiple chat rooms, ensuring that messages are directed solely to the intended recipients within each room. Say goodbye to the tedious task of implementing multicasting or broadcasting on your own; instead, leverage our user-friendly API to streamline the process.
rooms.ts
// send message to specific clientssender.message({ payload }).to([ws.id]);// send message to all clients in a roomsender.message({ payload }).toRoom(["room1", "room2"]);// send message to all clientssender.message({ payload }).broadcast();// create room with a client in it and choosen roomids.rooms.create(wid, rid);// close room and remove all clients in it from the rooms.rooms.close(rid);// let a client join a rooms.rooms.join(wid, rid);// let a client leave a rooms.rooms.leave(wid, rid);// get all clients in a rooms.rooms.list(rid);// get all rooms with clients in its.rooms.getRooms(wid);
test without headaches
The most advanced websocket testing tool you will ever need.
Take the stress out of quality assurance, so you can ensure the reliability of your websocket applications with ease thanks to autocompletion for payloads and the ability to send messages from your server via the testing client.
deploy in seconds
Its dead simple to get started with socks. Just run a single command to deploy your websocket server to the cloud and let us handle the rest.
$ socks deploy
Productive
Focus on building your application for your users, while leaving the infrastructure to the experts.
Reliable
No more worrying about your websocket server going down. We got you covered!
HTTP Emitter
A Server Message Emitter API for serverless and edge compute platforms, such as Cloudflare Workers or Vercel Edge Functions.
Scalable
Scale your application to millions of users without breaking a sweat.
Secure
Say goodbye to SSL certificate headaches and hello to secure websockets. by default
Performant
Our speed-optimized architecture ensures performant websockets out of the box, with low latency thanks to warmed-up serverless functions and distributed Redis clusters.
elevate monitoring to the next level
Seamlessly monitor websocket messages, detect errors, and optimize performance through enhanced insights
It's time to replace guesswork with data-driven optimisation and take your application to the next level. Unlock the full potential of websockets with our advanced analytics dashboard and gain deep insights into your traffic, allowing you to fine-tune your application for maximum efficiency.
Trace errors back to their source messages, ensuring a more reliable and fault-tolerant application. Uncover resource-intensive workflows with ease, enabling you to dedicate your efforts towards building rather than troubleshooting.
pricing makes the difference
You only pay for what you use, no hidden fees.
🫰 Free Tier
While our free version remains free indefinitely, it will come with a rate limit. We're actively working on finalizing our pricing details and will notify you as soon as they are available!
🚀 Scaler
This plan will allow you to scale your application to the moon!
🏢 Enterprise
Only for the most demanding users, this plan will allow you to scale your application to the moon and back to earth 3 times!