Currently in beta.
Brightsocket.io is an abstraction over Socket.io that makes managing socket connection pools and building websocket-based APIs easier than ever before!
It works like this:
1. Start with a Socket.io-compatible webserver.
In this case, we'll use express, just because most people are familiar with it.
;;;const app = ;const server = http;server;app;
2. Launch brightsocket and pass it the server.
The server object you pass in here is the same one you would pass to Socket.io if you were using it raw.
const api = ;
3. Create a connection channel.
The first thing our client side code is going to do when it connects is tell us which connection channel it wants to use. Connection channels are just arbitrarily named chunks of your API. Using channels allows you to do things like divide your API into partitions based on user type, and even require authentication right away.
This tiny snippet of code does many things. First, it sets up a connection listener. Whenever a new connection is detected, it will set up a subsequent listener for an incoming connection to identify which channel it would like to use. No other socket events will be acknowledged until the client side sends us this event. Using the Brightsocket client-side library, that would look like this:
// Client Side (using brightsocket.io-client library)const socket = ;socket;
On the server side, any connections that correctly identify a channel will get filtered into the
connect callback. That callback takes the connection itself, the payload that came through with the channel-identification action, and your raw webserver, in case you need it.
4. Define the rest of your API.
// We already wrote this line in step 3. You don't need to write it again.api;
Well that seems pretty cool.
But can you really write a full-featured API with it?
Yes. Here's an example using authentication and middleware:
// Server Side// Import all of our dependencies. In this case we'll use// brightsocket in connection with express and// jsonwebtoken for authentication.;;;;// Create our express server.const app = ;const server = http;// Create our brightsocket API builder.const api = ;// Normally this would be in the database, obviously,// but for simplicity sake, here's the auth record we'll// need the client side to match.const expectedCred =username: 'email@example.com'password: 'password';// And here's an example of a full user record. We'll// return this to the browser when it successfully// authenticates.const userRecord =id: 123firstname: 'John'lastname: 'Doe'email: 'firstname.lastname@example.org';// A random secret for signing json web tokens.const jwtSecret = 'How much wood would a wood chuck chuck?';// Ok, let's start up our webserver.server;// And let's serve up an html file when the user// connects via http.app;// Listen for new socket connections that identify// themselves as `MY_CHANNEL` connections.api;
And here's the corresponding client side code:
// Client Side (using brightsocket.io-client included on index.html)// We'll begin by turning on Socket.io.const socket = ;// And, based on our server side code, the first thing we// need to do is identify a channel.socket;// If at any point something goes wrong, we'll set up// a handler for when the server tells us we're not// authorized.socket;// Now let's set up a handler for when the server// lets us know that we've successfully authenticated.socket;// Might as well actually handle responses to those// other requests too.socket;socket;
Can I use the server library without the client library?
If you must. Here's what you'd need to do to get around the client library:
// using socket.iovar socket = ;socket;// From here, just use socket.on and socket.emit// instead of receive and send but it might get// a little clunky.
Can I combine pre-identified user channels?
You sure can. Here's an example:
const api = ;api;api;api;
In the above example, basic user and admin user channels each have their own console log that occurs whenever a
MESSAGE comes through. However, because each of these channels "extends" the
ALL_USERS channel, the
ALL_USERS console log will occur whenever a
MESSAGE comes through for either of the other channels as well.
And that's all there is to it. Super easy.