Sign In Register

How can we help you today?

Start a new topic

Script message performance


I am wondering if script messages ( Spark.message ) cost anything if it is sent to a player that is currently not online.

I am sending a messages to everyone in friend list or if in same clan when some player statistics change. For example a player leveled up, someone came online, started a game, etc.

Checking the API stream, I'm getting a lot of these messages sent to players that are not even online.

I'm sending the message this way:


            .setPlayerIds( recipentIds )





So it should be sending only via socket, expires immediately, so I thought this means that if there is no active websocket connection, it wont do anything.

Am I right to assume this? Or should I be checking if players are online before sending it?

couple of notes.
1.  how do you know they are not online?  GS detection of socket can be delayed by up to 30 minutes server side.  The easiest way to tell is if in the API stream , you see a SCHEDULED-GS_PLAYER_DISCONNECT message and THEN one of these social messages....

2.  Note that GS is doing some amount of work for every recipient - even if the player is offline.
3.  as per 2. above, the sending of that message to multiple recipients will block until all are sent (and it does each sequentially).  So if you have a large list of recipients, this can cause script timeout.

1) From the logs I checked, people receiving these messages who haven't been online for 2-3 weeks.

2-3) That was the point of the question. Are you sure about this? Is there any documentation that supplies this answer?

I dont have a large pool of users receiving the message, only at max like 30. Problem is the frequency. Every time something changes it sends messages to everyone on the friendlist and clan, and if none of them are online, its a waste. Unless GS handles it internally, and doesn't send to people who are unreachable

Create a last seen collection. Whenever someone logs in update the lastseen collection. 

Then when you sent messages get the playerIDs you added to the last seen collection and just extract the player IDs based on when suitable timeframe.

Example you can create a module and include it in the relevant cloud code

var lastSeen = Spark.runtimeCollection("lastseen");

var playerId = Spark.getPlayer().getPlayerId();



    { $set: {"playerId":playerId,"timestamp":new Date().getTime(),"date":new Date().toISOString()} },





// Then when you send out your message

// Define suitable offset time
// offset = a function that determines how much time you want to look back from in ms

var playersLastSeen = Spark.runtimeCollection("lastseen");

var allSeenPlayers = playersLastSeen.find({ "timestamp" : { $lt: offset } }).toArray()

//... get playerIDs into array of playerIds called playerIdArray..//...
Then send messages to relevant players only

var msg = Spark.message(null);


msg.setMessageData({"your message":"message object"});

msg.setExpireAfterHours(0) // set to zero if user message is to be sent but not replayed 


Login to post a comment