Mytosis WebSocket
Send real-time updates over websockets
What it's for
Mytosis defines a standard interface for network plugins, but it doesn't ship with any out of the box. mytosis-websocket
is one such adapter for websockets (I know, never would've guessed).
Here's what it does:
- Runs
uWebSockets
on the server because it's wicked fast - Connects with
reconnecting-websocket
on the front end because it's tiny and reconnects automatically - Supports binary payloads (
Blob
,File
, Node'sBuffer
,ArrayBuffer
) so you can finally send those cat pictures in real time
Here's what it won't do:
- Fall back to http polling.
WebSockets have great support (IE10+ in web units). If you're going retro or just really love Flash, write your own plugin. - Manage your data for you.
That's not how Mytosis works. You're in charge of your data. This plugin does nothing until you tell it to (in fact, these types of plugins don't even have a reference to your database). Take a look at theRouter
plugin type for more deets.
Installing
It's on npm as mytosis-websocket
. You probably know the drill, but here's something you can copy & paste.
# Yarn good yarn add mytosis-websocket # npm less good npm install --save mytosis-websocket
Using it
Note: there's this great websocket server on the internet that just shouts back whatever you send it. I use that URL in the examples:
ws://html5rocks.websocket.org/echo
There are two parts, the client and the server. They work best together, but it's not necessary.
By default, it'll use the global WebSocket
instance. Good for browsers...
const socket = 'ws://html5rocks.websocket.org/echo' const db =
Configuring the client
Need it in Node? Since there's no global WebSocket
constructor, you'll need to pass your own. Two good libraries are uws
and ws
. This library depends on uws
so if you don't really care, just pick that one.
const socket = 'ws://html5rocks.websocket.org/echo' WebSocket: WebSocket
When a websocket disconnects, it'll automatically try to reconnect. All of the reconnection stuff is configurable.
const defaultOptions = WebSocket: TheGlobalSocketConstructor protocols: undefined // It's the second param to `WebSocket` reconnectionDelayGrowFactor: 13 maxReconnectionDelay: 10000 minReconnectionDelay: 1500 connectionTimeout: 4000 maxRetries: Infinity debug: false
Note: Most of the above snippet is taken from the
reconnecting-websocket
readme.
Server
The server is mostly the same.
// Mind the appended "/server" const server = port: 8080 const db =
Boom! You've got a WebSocket server. High five, my friend! 🖐
Using the client and server simultaneously is a bit weird with Mytosis, you've gotta put it in two configs:
const db =
Setting options for the server
This library uses uws
is basically a drop-in replacement for ws
, so everything you can pass ws
you can pass to mytosis-websocket/server
. That's where the config.port
option comes from. Here's a link to some docs.
Another common option is config.server
, which allows you to pass your own http.Server
instance. Rather handy for use with express.
const server = const socketServer = server server
Examples
Watching for new connections:
const server = port: 8080 servermessages server server
All Mytosis servers will work like this. It's part of the spec.
Here's how you'd listen for updates and send changes:
const server = port: 8080 const db =
There you go!
Support
Real-time updates are still a new feature of Mytosis. If you find a problem, please post an issue. It could influence the future of the plugin spec. Seriously, don't be shy.
Questions and comments are also good for GitHub issues.