21 Jan 2018

Sending Data To Roblox Game Servers In Real Time Using Http Long Polling


Let’s say you want to notify a game server immediately when some state outside of the game server changes. Here are some example use cases:

  • A player is sending a chat message to a player in another server
  • Roblox releases a new catalog item and you want to notify all your players

You can use a technique called HTTP long polling to accomplish this. The basic idea is that you send an HTTP request to your web server. Instead of the web server responding immediately, it waits until there is an update and then sends back a response. If the request takes longer than say 60 seconds the server will abort the request anyway.

Here is an example of what the server-side code might look like (node.js):

var EventEmitter = require('events').EventEmitter
var messageBus = new EventEmitter()

var express = require('express')
var app = express()

var bodyParser = require('body-parser')

app.set('port', (process.env.PORT || 80))

app.get('/', function(req, res) {
	function listener(data) {
		res.json({ updates: true, data: data})
	messageBus.once('message', listener)
    setTimeout(function() {
    	messageBus.removeListener('message', listener)
    	res.json({ updates: false, data: null })
	}, 30000)

app.post('/', function(req, res) {
	messageBus.emit('message', res.body)


And here is the Roblox Lua code:

local httpService = game:GetService("HttpService")
local URL = "https://api.example.com/"

while true do
	local success, result = pcall(function() return httpService:GetAsync(url)) end)
	if success then
		local data = httpService:JSONDecode(result)
		if data.updates then
		warn("error while fetching")