API factory for a realtime chat site I was told I'm using factories wrong, but I'm not so sure. Any ideas? (I know I should be using GET; I've only just noticed that)

app.factory('apiFactory', ['$http', function($http){
    var apiFactory = {};

    apiFactory.initUser = function(){
        return  $http({
            method: "POST",
            url: "api/new_user.php",
            headers: {"Content-Type": "application/x-www-form-urlencoded"}
        });
    };

    apiFactory.hostOrClient = function(session){
        return $http({
            method: "POST",
            url: "api/sessionhostclient.php",
            data: $.param({"session":session}),
            headers:{"Content-Type": "application/x-www-form-urlencoded"}
        });
    };

    apiFactory.hasPlayerJoined = function(session){
        return $http({
            method: "POST",
            url: "api/sessionstatus.php",
            data: $.param({"session":session}),
            headers: {"Content-Type": "application/x-www-form-urlencoded"}
        });
    };

    apiFactory.sendMessage = function(session, message){
        return $http({
            method: "POST",
            url: "api/addmessage.php",
            data: $.param({"message":message, "session":session}),
            headers: {"Content-Type": "application/x-www-form-urlencoded"}
        });
    };

    apiFactory.checkMessages = function(session, id){
        return $http({
            method: "POST",
            url: "api/getmessages.php",
            data: $.param({"session":session, "id":id}),
            headers: {"Content-Type": "application/x-www-form-urlencoded"}
        });
    };

    apiFactory.getAllMessages = function(session) {
        if(session == null){
            return;
        }

        return $http({
            method:"POST",
            url:"api/getallmessages.php",
            data: $.param({"session":session}),
            headers:{"Content-Type": "application/x-www-form-urlencoded"}
        });
    } ;

    apiFactory.getMessagesFrom = function(session, id){
        return $http({
            method: "POST",
            url: "api/getmessages.php",
            data: $.param({"session":session, "id":id}),
            headers: {"Content-Type": "application/x-www-form-urlencoded"}
        });
    };

    return apiFactory;
}]);
James Heald said:


  I was told I'm using factories wrong, but I'm not so sure.


The people telling you that are technically correct. A "factory" is an object or method whose sole purpose for existence is to create other objects. Factories are generally used when you want to create a concrete sub type, but return either an interface or a common parent type.

From Wikipedia: Factory (object-oriented programming)


  In class-based programming, a factory is an abstraction of a constructor of a class, while in prototype-based programming a factory is an abstraction of a prototype object. A constructor is concrete in that it creates objects as instances of a single class, and by a specified process (class instantiation), while a factory can create objects by instantiating various classes, or by using other allocation schemes such as an object pool. A prototype object is concrete in that it is used to create objects by being cloned, while a factory can create objects by cloning various prototypes, or by other allocation schemes.


Your apiFactory object is technically returning "new" objects, but since it is making web service calls, you've actually created a "service object".

More info: AngularJS Services.

Really the problem is the name "apiFactory" because the apiFactory variable itself is not a factory. It is a service. A more appropriate name might be ChatService.

The function that returns apiFactory is actually the factory, whereas apiFactory is actually the service.