You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.4 KiB

3 months ago
  1. # js-message
  2. Normalized JS & JSON Message and event Protocol for node.js, vanilla.js (plain old javascript), react.js, websockets, rest api's, node-ipc, and any other protocol that might use a js object and or a JSON string.
  3. js-message allows for seamless conversion of JSON messages and events to JS objects for a normalized implementation on the server and in the client without needing to concern yourself with JSON intermediaries and custom parsers.
  4. Things are just easier when you normalize them.
  5. npm js-message info : [See npm trends and stats for js-message](http://npm-stat.com/charts.html?package=js-message&author=&from=&to=)
  6. ![js-message npm version](https://img.shields.io/npm/v/js-message.svg) ![supported node version for js-message](https://img.shields.io/node/v/js-message.svg) ![total npm downloads for js-message](https://img.shields.io/npm/dt/js-message.svg) ![monthly npm downloads for js-message](https://img.shields.io/npm/dm/js-message.svg) ![npm licence for js-message](https://img.shields.io/npm/l/js-message.svg)
  7. ` npm install --save js-message `
  8. [![RIAEvangelist](https://avatars3.githubusercontent.com/u/369041?v=3&s=100)](https://github.com/RIAEvangelist)
  9. GitHub info :
  10. [![js-message GitHub Release](https://img.shields.io/github/release/RIAEvangelist/js-message.svg) ![GitHub license js-message license](https://img.shields.io/github/license/RIAEvangelist/js-message.svg) ![open issues for js-message on GitHub](https://img.shields.io/github/issues/RIAEvangelist/js-message.svg)](http://riaevangelist.github.io/js-message/)
  11. [js-message site](http://riaevangelist.github.io/js-message/)
  12. |method or key |type |mutable|description|
  13. |---------------|-------|-------|-----------|
  14. |type |String |true |the type of message|
  15. |data |Object |true |the message data or payload|
  16. |load |func |false |load a message from JSON, this will return a message with the type of error if not valid JSON|
  17. |JSON |String |not by user|JSON representation of the message|
  18. ### Creating a Message Object
  19. ```javascript
  20. //commonjs
  21. var Message=require('js-message');
  22. //plain old javascript
  23. <script src='js-message-vanilla.js' />
  24. var myMessage=new Message;
  25. myMessage.type='message or event type';
  26. myMessage.data.something='something';
  27. myMessage.data.stuff=[1,2,3,4,5]
  28. console.log(myMessage.JSON);
  29. ```
  30. ### Creating a Message From JSON
  31. ```javascript
  32. //commonjs
  33. var Message=require('js-message');
  34. //plain old javascript
  35. <script src='js-message-vanilla.js' />
  36. //lets say we have the above example running on
  37. //a websocket server sending js-messages as JSON
  38. //
  39. //and lets say this is the client in the browser
  40. ws.on(
  41. 'message',
  42. handleMessage
  43. );
  44. handleMessage(e){
  45. var message=new Message;
  46. message.load(e.data);
  47. console.log(message.type, message.data);
  48. }
  49. ```
  50. ### Sending a Message Object via WebSocket
  51. ```javascript
  52. //commonjs
  53. var Message=require('js-message');
  54. //plain old javascript
  55. <script src='js-message-vanilla.js' />
  56. //client example, but works the same on server too!
  57. var ws=new WebSocket('ws://myawesomeWS:8000');
  58. var myMessage=new Message;
  59. myMessage.type='setUsername';
  60. myMessage.data.username='sideshow bob';
  61. ws.send(myMessage.JSON);
  62. ```
  63. ---
  64. This work is licenced via the [DBAD Public Licence](http://www.dbad-license.org/).