Giter Site home page Giter Site logo

moleculer-socketio's Introduction

Moleculer logo

moleculer-socketio NPM version

Description

Manage socket.io's namespaces like services and actions.

Project example

You can find an example of moleculer-socketio and moleculer-nextjs at moleculer-nextjs-socketio-example

Install

$ npm install moleculer-socketio --save

USAGE

you can use moleculer-socketio manage events like actions in services, in socket.io it's called namespaces so you must add it into your service.

In socket.io, the root channel is / but you can define multiple namespaces !

namespaces: {
    "/": {
        //	root namespace
        hello(ctx) {
            this.logger.info("Hello there");
            return "Get this data!";
        }
    },
    "/private": {
		//	another namespace
        hi(ctx) {
            this.logger.info("You're not the droid I'm looking for.")
        }
    }
}

On the client side you could connect to those namespaces :

const io = require("socket.io-client");

const root = io("http://ip:port");
root.emit("hello");
root.on("hello", (data) => {
    console.log("hello return", data);
});

const private = io("http://ip:port/private");
private.emit("hi")

Events

Every events for each namespaces can be code differently :

namespaces: {
    "/": {
        eventA(ctx) {
            return "somedata"
        },
        eventB: {
            //	You can specify parameter validation
            //	Just like actions
            params: {
                parameterA: "string",
                parameterB: "number"
            },
            async handler(ctx) {
				return "somedata again";
            }
       },
       eventC: "service.action",
       eventD: [
		   "service.action",
           function(ctx) {
               return "another data"
           },
           "service.action"
       ]
    }
}

When you return data from an event, it will emit an event with it.

Middlewares

You may want to add some conditions to validate the execution of your actions. You can use a middleware for this case.

namespaces: {
    "/private": {
        
        middlewares: [
            function(ctx) {
                if (!ctx.params.crypted) {
                    throw new Error("You're missing crypted parameter");
                }
            }  
        ],
        
        deleteAllUsers: {
            middlewares: [
                function(ctx) {
                    if (!ctx.params.isAdmin) {
                        throw new Error("No, you're not an admin");
                    }
                },
                function(ctx) {
                    if (ctx.params.username != "Icebob") {
                        throw new Error("Sorry, you're not enough badass");
                    }
                }
            ],
            handler(ctx) {
            	return "Destroy everything";    
            }
        }
        
    }
}

Custom response

One more thing, you can also manage custom responses.

const SocketIO = require("moleculer-socketio");

module.exports = {
	name: "socket-service",

	mixins: [ SocketIO ],

	settings: {
		port: 5000,
        options: {
			//	Socket.io options
		},
		response: (event, error, results) => {
			// Response Templated
			let payload = {};
			if (!error) {
				if (results.length > 1) {
					results.map((r, index) => {
						payload[event.actions[index]] = r;
					});
				} else {
					payload = results[0];
				}
			}
			return {
				error: error,
				payload: payload
			};
		},
	},
    
};

EXAMPLE

const SocketIO = require("moleculer-socketio");

module.exports = {
	name: "socket-service",

	mixins: [ SocketIO ],

	settings: {
		port: 5000,
        options: {
			//	Socket.io options
		},
	},

    actions: {
       
    },

    namespaces: {
        "/": {
            connection(ctx) {
                this.logger.info("Someone is connected");
                ctx.socket.emit("hello", "Welcome to socket.io");
            },
            
            
            
            sendMessage: {
                params: {
                    message: "string"
                },
                async handler(ctx) {
					this.logger.info(ctx.params.message);
                    return true;
                }
            },
            
        },
        
        "/admin": {
            
            middlewares: [
                function(ctx) {
                    if (!ctx.params.isAdmin) {
                        throw new Error("Sorry you can't connect here");
                    }
                }
            ],
            
            connection: {
                middlewares: [
                    function(ctx) {
						if (ctx.params.isAdmin && ctx.) {
                            throw new Error("Sorry you can't connect here");
                        }
                    }
                ],
                async handler(ctx) {
                     this.logger.info("An administrator is connected");
                	ctx.socket.emit("hello", "Welcome to socket.io on admin side");
                }
            }
        }
    },

	methods: {

		onCreated(io) {
			console.log("socket-io.service - onCreated");
			this.io = io;
            this.io.on("oldWay", (socket, data) => {
                this.logger.info("oldWay - ", data);
                socket.emit("oldWay", "You can still to this");
            });
		},

	},

}

This should be on client side

import io from 'socket.io-client';


this.socket = io("http://localhost:5000");

this.socket.on("all", (data) => {
  console.log("client -- ", data);
});

this.socket.on('timer', timestamp => console.log("TIMER IS", timestamp));
this.socket.emit('subscribeToTimer', 1000);

moleculer-socketio's People

Contributors

davidroman0o avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

moleculer-socketio's Issues

Documentation not clear

Your documentation is not comprehensive enough, I had to look at the code to write a proper service, also, the code in your documentation is not well-formatted, just good idea, good work, but crappy documentation, please just consider working on that.
On service reload during debugging, it looks like the server is not well shut down, I had to call this.io.close() manually inside stopped function. Also, consider improving the code, might fork when I get time

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.