AJAXy Websockets
Familiar with node http and Express web servers? AJAXy Websockets enables you to do things like this!
/***** On the server *****/var io = server;var socketRouter = io; // This is using websockets, not HTTP!socketRouter; /***** On the client *****/// Create the socketvar socket = socketURL: 'ws://127.0.0.1:3000';socketstart; // When user clicks the button, get data for ID 1234567 from websocket!;
Gracefullly degrade to AJAX requests if sockets aren't available
The beautiful part about all this is that you can assign the exact same handler method for both sockets and AJAX!
var { ifreqparamsid // Got an ID! Send back an "object" res; else res; }; // Handle incoming socket requestssocketRouter;// Gracefully degrade to AJAXserver;
Dev Note: This is still in it's early stages so take care if you plan to use this in production apps. See the roadmap below for what needs to be implemented.
Dependencies
The full setup
This solution does require the use of socket.io (shouldn't be a huge deal since it's the most popular sockets library on NPM). This can be used with or without Express.
On the server:
var AjaxySocketRouter = ; // Instantiate a basic web servervar server = ; // Require socket.io and pass it to an instance of ajaxy socketsvar io = server;var socketRouter = io;
On the client:
Websockets routing! <!-- load in Socket.io client --> <!-- load in AjaxySocket --> Gimme socket data!
That's it! It can also be used with express.
var express = ; // Setup express appvar app = ;app; // Now with express setup, just create an HTTP webserver // (passing it express app) and the rest is the same as before!var server = ; // Require socket.io and pass it to an instance of ajaxy socketsvar io = server;var socketRouter = io;
Need to secure sockets with authentication using things like passport for Socket.io? No problem!
// Basic socket.io setupvar io = server; // Use the socket.io bundle and passport libsvar socketIOBundle = ;var socketIOPassport = ;// Setup authio;io;io;io; // Create AjaxySocketRouter the regular wayvar ajaxySocketsRouter = io;
API
Server side AjaxySocketsRouter
constructor
- Params
- Server server instance
get
Setup a handler for GET type requests
- Params:
- string route
- function handler
post
Setup a handler for POST type requests
- Params:
- string route
- function handler
patch
Setup a handler for PATCH type requests
- Params:
- string route
- function handler
del
Setup a handler for DELETE type requests
- Params:
- string route
- function handler
Client side AjaxySocket
constructor
- Params
- string socketURL - The URI for websocket server (ex. ws://127.0.0.1:8888)
start
Inits the websocket. Must be called when you want the socket to connect to the server.
- Params NONE
get
Make a GET type request to the socket server
- Params
- string url - The request url
- function callback - The callback which is executed when the socket receives a response
post
Make a POST type request to the socket server
- Params
- string url - The request url
- function callback - The callback which is executed when the socket receives a response
patch
Make a PATCH type request to the socket server
- Params
- string url - The request url
- function callback - The callback which is executed when the socket receives a response
del
Make a DELETE type request to the socket server
- Params
- string url - The request url
- function callback - The callback which is executed when the socket receives a response
ajaxyRequest
Makes a request based on the options object passed to it (similiar to jQuery $.ajax()
method)
- Params
- object options - An object with the following properties:
- string url
- string method - The request type (GET, POST, etc)
- object data - (optional) The data to be passed with the request
- function callback - Exectued when the response is received
- object options - An object with the following properties:
Roadmap
- Add good error handling
- Promises!
- Add various methods to request/response classes that are available in Express req/response