Skip to main contentS2 streams make for a great serverless, reliable alternative to WebSockets.
Reliability
Every message on a stream has a deterministic ordering expressed by its sequence number. If a client reading from the stream disconnects, it can reconnect where it left off by starting a new read from the last sequence number it received previously.
Similarly, a client who is writing to a stream can use concurrency controls like match_seq_num to ensure that a message is not sent twice.
A bidirectional WebSocket connection can be simulated either by using two S2 streams in an input/output channel fashion, or a single stream can be written to by both the client and the server.
Auditability
Records in S2 are durable, not ephemeral like in a WebSocket connection. To clear messages, a stream has to either explicitly trim, or you can set a time-based retention policy when creating the stream. This means you can audit the history of a streaming session.
Access
S2 is directly accessible from the browser via the REST API and our TypeScript SDK. Streaming reads can be accomplished via Server-Sent Events.
Serverless
If you’re using S2 streams directly to communicate instead of a stateful WebSockets connection, all of the streaming is handled by S2. Your site only needs to facilitate the initial connection between your client and the S2 resources – for instance, by issuing and providing an access token and S2 stream URI to your client.
The worker function powering your service is then free to consume from and write to the stream as actively or lazily as it sees fit.
Try it out
Check out our playground to experiment with reading and writing to S2 streams directly from your browser.